完整项目链接:

https://download.csdn.net/download/qq_41900846/87567939

使用VGG16-places365模型对图片进行目标检测,标出图片中的场景

# -*- coding: utf-8 -*-
'''VGG16-places365 model for Keras# Reference:
- [Places: A 10 million Image Database for Scene Recognition](http://places2.csail.mit.edu/PAMI_places.pdf)
'''from __future__ import division, print_function
import osimport warnings
import numpy as npfrom keras import backend as K
from keras.layers import Input
from keras.layers.core import Activation, Dense, Flatten
from keras.layers.pooling import MaxPooling2D
from keras.models import Model
from keras.layers import Conv2D
from keras.regularizers import l2
from keras.layers.core import Dropout
from keras.layers import GlobalAveragePooling2D
from keras.layers import GlobalMaxPooling2D
from keras_applications.imagenet_utils import _obtain_input_shape
from keras.engine.topology import get_source_inputs
from keras.utils.data_utils import get_file
from keras.utils import layer_utils
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
# from urllib.request import urlopen
import numpy as np
import pandas as pd
from PIL import Image
from cv2 import resize
import glob
from tqdm import tqdm
# WEIGHTS_PATH = 'https://github.com/GKalliatakis/Keras-VGG16-places365/releases/download/v1.0/vgg16-places365_weights_tf_dim_ordering_tf_kernels.h5'
# WEIGHTS_PATH_NO_TOP = 'https://github.com/GKalliatakis/Keras-VGG16-places365/releases/download/v1.0/vgg16-places365_weights_tf_dim_ordering_tf_kernels_notop.h5'
WEIGHTS_PATH = 'model/vgg16-places365_weights_tf_dim_ordering_tf_kernels.h5'
WEIGHTS_PATH_NO_TOP = 'model/vgg16-places365_weights_tf_dim_ordering_tf_kernels_notop.h5'def VGG16_Places365(include_top=True, weights='places',input_tensor=None, input_shape=None,pooling=None,classes=365):"""Instantiates the VGG16-places365 architecture.Optionally loads weights pre-trainedon Places. Note that when using TensorFlow,for best performance you should set`image_data_format="channels_last"` in your Keras configat ~/.keras/keras.json.The model and the weights are compatible with bothTensorFlow and Theano. The data formatconvention used by the model is the onespecified in your Keras config file.# Argumentsinclude_top: whether to include the 3 fully-connectedlayers at the top of the network.weights: one of `None` (random initialization),'places' (pre-training on Places),or the path to the weights file to be loaded.input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)to use as image input for the model.input_shape: optional shape tuple, only to be specifiedif `include_top` is False (otherwise the input shapehas to be `(224, 224, 3)` (with `channels_last` data format)or `(3, 224, 244)` (with `channels_first` data format).It should have exactly 3 inputs channels,and width and height should be no smaller than 48.E.g. `(200, 200, 3)` would be one valid value.pooling: Optional pooling mode for feature extractionwhen `include_top` is `False`.- `None` means that the output of the model will bethe 4D tensor output of thelast convolutional layer.- `avg` means that global average poolingwill be applied to the output of thelast convolutional layer, and thusthe output of the model will be a 2D tensor.- `max` means that global max pooling willbe applied.classes: optional number of classes to classify imagesinto, only to be specified if `include_top` is True, andif no `weights` argument is specified.# ReturnsA Keras model instance.# RaisesValueError: in case of invalid argument for `weights`, or invalid input shape"""if not (weights in {'places', None} or os.path.exists(weights)):raise ValueError('The `weights` argument should be either ''`None` (random initialization), `places` ''(pre-training on Places), ''or the path to the weights file to be loaded.')if weights == 'places' and include_top and classes != 365:raise ValueError('If using `weights` as places with `include_top`'' as true, `classes` should be 365')# Determine proper input shapeinput_shape = _obtain_input_shape(input_shape,default_size=224,min_size=48,data_format=K.image_data_format(),require_flatten =include_top)if input_tensor is None:img_input = Input(shape=input_shape)else:if not K.is_keras_tensor(input_tensor):img_input = Input(tensor=input_tensor, shape=input_shape)else:img_input = input_tensor# Block 1x = Conv2D(filters=64, kernel_size=3, strides=(1, 1), padding='same',kernel_regularizer=l2(0.0002),activation='relu', name='block1_conv1')(img_input)x = Conv2D(filters=64, kernel_size=3, strides=(1, 1), padding='same',kernel_regularizer=l2(0.0002),activation='relu', name='block1_conv2')(x)x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name="block1_pool", padding='valid')(x)# Block 2x = Conv2D(filters=128, kernel_size=3, strides=(1, 1), padding='same',kernel_regularizer=l2(0.0002),activation='relu', name='block2_conv1')(x)x = Conv2D(filters=128, kernel_size=3, strides=(1, 1), padding='same',kernel_regularizer=l2(0.0002),activation='relu', name='block2_conv2')(x)x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name="block2_pool", padding='valid')(x)# Block 3x = Conv2D(filters=256, kernel_size=3, strides=(1, 1), padding='same',kernel_regularizer=l2(0.0002),activation='relu', name='block3_conv1')(x)x = Conv2D(filters=256, kernel_size=3, strides=(1, 1), padding='same',kernel_regularizer=l2(0.0002),activation='relu', name='block3_conv2')(x)x = Conv2D(filters=256, kernel_size=3, strides=(1, 1), padding='same',kernel_regularizer=l2(0.0002),activation='relu', name='block3_conv3')(x)x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name="block3_pool", padding='valid')(x)# Block 4x = Conv2D(filters=512, kernel_size=3, strides=(1, 1), padding='same',kernel_regularizer=l2(0.0002),activation='relu', name='block4_conv1')(x)x = Conv2D(filters=512, kernel_size=3, strides=(1, 1), padding='same',kernel_regularizer=l2(0.0002),activation='relu', name='block4_conv2')(x)x = Conv2D(filters=512, kernel_size=3, strides=(1, 1), padding='same',kernel_regularizer=l2(0.0002),activation='relu', name='block4_conv3')(x)x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name="block4_pool", padding='valid')(x)# Block 5x = Conv2D(filters=512, kernel_size=3, strides=(1, 1), padding='same',kernel_regularizer=l2(0.0002),activation='relu', name='block5_conv1')(x)x = Conv2D(filters=512, kernel_size=3, strides=(1, 1), padding='same',kernel_regularizer=l2(0.0002),activation='relu', name='block5_conv2')(x)x = Conv2D(filters=512, kernel_size=3, strides=(1, 1), padding='same',kernel_regularizer=l2(0.0002),activation='relu', name='block5_conv3')(x)x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name="block5_pool", padding='valid')(x)if include_top:# Classification blockx = Flatten(name='flatten')(x)x = Dense(4096, activation='relu', name='fc1')(x)x = Dropout(0.5, name='drop_fc1')(x)x = Dense(4096, activation='relu', name='fc2')(x)x = Dropout(0.5, name='drop_fc2')(x)x = Dense(365, activation='softmax', name="predictions")(x)else:if pooling == 'avg':x = GlobalAveragePooling2D()(x)elif pooling == 'max':x = GlobalMaxPooling2D()(x)# Ensure that the model takes into account# any potential predecessors of `input_tensor`.if input_tensor is not None:inputs = get_source_inputs(input_tensor)else:inputs = img_input# Create model.model = Model(inputs, x, name='vgg16-places365')# load weightsif weights == 'places':if include_top:weights_path = WEIGHTS_PATHelse:weights_path = WEIGHTS_PATH_NO_TOPmodel.load_weights(weights_path)if K.backend() == 'theano':layer_utils.convert_all_kernels_in_model(model)if K.image_data_format() == 'channels_first':if include_top:maxpool = model.get_layer(name='block5_pool')shape = maxpool.output_shape[1:]dense = model.get_layer(name='fc1')layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first')if K.backend() == 'tensorflow':warnings.warn('You are using the TensorFlow backend, yet you ''are using the Theano ''image data format convention ''(`image_data_format="channels_first"`). ''For best performance, set ''`image_data_format="channels_last"` in ''your Keras config ''at ~/.keras/keras.json.')elif weights is not None:model.load_weights(weights)return model
def sigmoid(x):return 1/(1+np.exp(-x))if __name__ == '__main__':Threshold = 0.51  # 自己要卡阈值pred_array = np.empty((0,6),dtype=float)TEST_PATH = "/放图片的路径/*.jpg"f =open("/记录图片名字、图片中的id、图片中的类别名字的tsv,一共三列/_places365.tsv",'w')model = VGG16_Places365(weights='places')img_file = glob.glob(TEST_PATH)file_name = 'categories_places365.txt'classes = list()with open(file_name) as class_file:for line in class_file:classes.append(line.strip().split(' ')[0][3:])classes = tuple(classes)model = VGG16_Places365(weights='places')img_file = glob.glob(TEST_PATH)broken_img_num = 0for img in tqdm(img_file, total=len(img_file)):tagid = []tagname =[]img_name = os.path.basename(img)if os.path.getsize(img):try:image = Image.open(img)image = image.convert("RGB")image = np.array(image, dtype=np.uint8)image = resize(image, (224, 224))image = np.expand_dims(image, 0)predictions_to_return = 5#import pdb;pdb.set_trace()preds = model.predict(image)[0]top_preds = np.argsort(preds)[::-1][0:predictions_to_return]logit_cls = sigmoid(preds)tagid = np.where(logit_cls>Threshold)[0].tolist()tagname = [classes[tid] for tid in tagid]# print( f'{img_name}\t{str(tagid)}\t{str(tagname)}\n')# exit()except Exception as e:broken_img_num += 1# print( f'{img_name}\t{str(tagid)}\t{str(tagname)}\n')# exit()f.write(f'{img_name}\t{str(tagid)}\t{str(tagname)}\n')f.flush()print(f'总共损坏图像={str(broken_img_num)}')

模型

  • vgg16-places365_weights_tf_dim_ordering_tf_kernels.h5
  • vgg16-places365_weights_tf_dim_ordering_tf_kernels_notop.h5

类别文档:categories_places365.txt

/a/airfield 0
/a/airplane_cabin 1
/a/airport_terminal 2
/a/alcove 3
/a/alley 4
/a/amphitheater 5
/a/amusement_arcade 6
/a/amusement_park 7
/a/apartment_building/outdoor 8
/a/aquarium 9
/a/aqueduct 10
/a/arcade 11
/a/arch 12
/a/archaelogical_excavation 13
/a/archive 14
/a/arena/hockey 15
/a/arena/performance 16
/a/arena/rodeo 17
/a/army_base 18
/a/art_gallery 19
/a/art_school 20
/a/art_studio 21
/a/artists_loft 22
/a/assembly_line 23
/a/athletic_field/outdoor 24
/a/atrium/public 25
/a/attic 26
/a/auditorium 27
/a/auto_factory 28
/a/auto_showroom 29
/b/badlands 30
/b/bakery/shop 31
/b/balcony/exterior 32
/b/balcony/interior 33
/b/ball_pit 34
/b/ballroom 35
/b/bamboo_forest 36
/b/bank_vault 37
/b/banquet_hall 38
/b/bar 39
/b/barn 40
/b/barndoor 41
/b/baseball_field 42
/b/basement 43
/b/basketball_court/indoor 44
/b/bathroom 45
/b/bazaar/indoor 46
/b/bazaar/outdoor 47
/b/beach 48
/b/beach_house 49
/b/beauty_salon 50
/b/bedchamber 51
/b/bedroom 52
/b/beer_garden 53
/b/beer_hall 54
/b/berth 55
/b/biology_laboratory 56
/b/boardwalk 57
/b/boat_deck 58
/b/boathouse 59
/b/bookstore 60
/b/booth/indoor 61
/b/botanical_garden 62
/b/bow_window/indoor 63
/b/bowling_alley 64
/b/boxing_ring 65
/b/bridge 66
/b/building_facade 67
/b/bullring 68
/b/burial_chamber 69
/b/bus_interior 70
/b/bus_station/indoor 71
/b/butchers_shop 72
/b/butte 73
/c/cabin/outdoor 74
/c/cafeteria 75
/c/campsite 76
/c/campus 77
/c/canal/natural 78
/c/canal/urban 79
/c/candy_store 80
/c/canyon 81
/c/car_interior 82
/c/carrousel 83
/c/castle 84
/c/catacomb 85
/c/cemetery 86
/c/chalet 87
/c/chemistry_lab 88
/c/childs_room 89
/c/church/indoor 90
/c/church/outdoor 91
/c/classroom 92
/c/clean_room 93
/c/cliff 94
/c/closet 95
/c/clothing_store 96
/c/coast 97
/c/cockpit 98
/c/coffee_shop 99
/c/computer_room 100
/c/conference_center 101
/c/conference_room 102
/c/construction_site 103
/c/corn_field 104
/c/corral 105
/c/corridor 106
/c/cottage 107
/c/courthouse 108
/c/courtyard 109
/c/creek 110
/c/crevasse 111
/c/crosswalk 112
/d/dam 113
/d/delicatessen 114
/d/department_store 115
/d/desert/sand 116
/d/desert/vegetation 117
/d/desert_road 118
/d/diner/outdoor 119
/d/dining_hall 120
/d/dining_room 121
/d/discotheque 122
/d/doorway/outdoor 123
/d/dorm_room 124
/d/downtown 125
/d/dressing_room 126
/d/driveway 127
/d/drugstore 128
/e/elevator/door 129
/e/elevator_lobby 130
/e/elevator_shaft 131
/e/embassy 132
/e/engine_room 133
/e/entrance_hall 134
/e/escalator/indoor 135
/e/excavation 136
/f/fabric_store 137
/f/farm 138
/f/fastfood_restaurant 139
/f/field/cultivated 140
/f/field/wild 141
/f/field_road 142
/f/fire_escape 143
/f/fire_station 144
/f/fishpond 145
/f/flea_market/indoor 146
/f/florist_shop/indoor 147
/f/food_court 148
/f/football_field 149
/f/forest/broadleaf 150
/f/forest_path 151
/f/forest_road 152
/f/formal_garden 153
/f/fountain 154
/g/galley 155
/g/garage/indoor 156
/g/garage/outdoor 157
/g/gas_station 158
/g/gazebo/exterior 159
/g/general_store/indoor 160
/g/general_store/outdoor 161
/g/gift_shop 162
/g/glacier 163
/g/golf_course 164
/g/greenhouse/indoor 165
/g/greenhouse/outdoor 166
/g/grotto 167
/g/gymnasium/indoor 168
/h/hangar/indoor 169
/h/hangar/outdoor 170
/h/harbor 171
/h/hardware_store 172
/h/hayfield 173
/h/heliport 174
/h/highway 175
/h/home_office 176
/h/home_theater 177
/h/hospital 178
/h/hospital_room 179
/h/hot_spring 180
/h/hotel/outdoor 181
/h/hotel_room 182
/h/house 183
/h/hunting_lodge/outdoor 184
/i/ice_cream_parlor 185
/i/ice_floe 186
/i/ice_shelf 187
/i/ice_skating_rink/indoor 188
/i/ice_skating_rink/outdoor 189
/i/iceberg 190
/i/igloo 191
/i/industrial_area 192
/i/inn/outdoor 193
/i/islet 194
/j/jacuzzi/indoor 195
/j/jail_cell 196
/j/japanese_garden 197
/j/jewelry_shop 198
/j/junkyard 199
/k/kasbah 200
/k/kennel/outdoor 201
/k/kindergarden_classroom 202
/k/kitchen 203
/l/lagoon 204
/l/lake/natural 205
/l/landfill 206
/l/landing_deck 207
/l/laundromat 208
/l/lawn 209
/l/lecture_room 210
/l/legislative_chamber 211
/l/library/indoor 212
/l/library/outdoor 213
/l/lighthouse 214
/l/living_room 215
/l/loading_dock 216
/l/lobby 217
/l/lock_chamber 218
/l/locker_room 219
/m/mansion 220
/m/manufactured_home 221
/m/market/indoor 222
/m/market/outdoor 223
/m/marsh 224
/m/martial_arts_gym 225
/m/mausoleum 226
/m/medina 227
/m/mezzanine 228
/m/moat/water 229
/m/mosque/outdoor 230
/m/motel 231
/m/mountain 232
/m/mountain_path 233
/m/mountain_snowy 234
/m/movie_theater/indoor 235
/m/museum/indoor 236
/m/museum/outdoor 237
/m/music_studio 238
/n/natural_history_museum 239
/n/nursery 240
/n/nursing_home 241
/o/oast_house 242
/o/ocean 243
/o/office 244
/o/office_building 245
/o/office_cubicles 246
/o/oilrig 247
/o/operating_room 248
/o/orchard 249
/o/orchestra_pit 250
/p/pagoda 251
/p/palace 252
/p/pantry 253
/p/park 254
/p/parking_garage/indoor 255
/p/parking_garage/outdoor 256
/p/parking_lot 257
/p/pasture 258
/p/patio 259
/p/pavilion 260
/p/pet_shop 261
/p/pharmacy 262
/p/phone_booth 263
/p/physics_laboratory 264
/p/picnic_area 265
/p/pier 266
/p/pizzeria 267
/p/playground 268
/p/playroom 269
/p/plaza 270
/p/pond 271
/p/porch 272
/p/promenade 273
/p/pub/indoor 274
/r/racecourse 275
/r/raceway 276
/r/raft 277
/r/railroad_track 278
/r/rainforest 279
/r/reception 280
/r/recreation_room 281
/r/repair_shop 282
/r/residential_neighborhood 283
/r/restaurant 284
/r/restaurant_kitchen 285
/r/restaurant_patio 286
/r/rice_paddy 287
/r/river 288
/r/rock_arch 289
/r/roof_garden 290
/r/rope_bridge 291
/r/ruin 292
/r/runway 293
/s/sandbox 294
/s/sauna 295
/s/schoolhouse 296
/s/science_museum 297
/s/server_room 298
/s/shed 299
/s/shoe_shop 300
/s/shopfront 301
/s/shopping_mall/indoor 302
/s/shower 303
/s/ski_resort 304
/s/ski_slope 305
/s/sky 306
/s/skyscraper 307
/s/slum 308
/s/snowfield 309
/s/soccer_field 310
/s/stable 311
/s/stadium/baseball 312
/s/stadium/football 313
/s/stadium/soccer 314
/s/stage/indoor 315
/s/stage/outdoor 316
/s/staircase 317
/s/storage_room 318
/s/street 319
/s/subway_station/platform 320
/s/supermarket 321
/s/sushi_bar 322
/s/swamp 323
/s/swimming_hole 324
/s/swimming_pool/indoor 325
/s/swimming_pool/outdoor 326
/s/synagogue/outdoor 327
/t/television_room 328
/t/television_studio 329
/t/temple/asia 330
/t/throne_room 331
/t/ticket_booth 332
/t/topiary_garden 333
/t/tower 334
/t/toyshop 335
/t/train_interior 336
/t/train_station/platform 337
/t/tree_farm 338
/t/tree_house 339
/t/trench 340
/t/tundra 341
/u/underwater/ocean_deep 342
/u/utility_room 343
/v/valley 344
/v/vegetable_garden 345
/v/veterinarians_office 346
/v/viaduct 347
/v/village 348
/v/vineyard 349
/v/volcano 350
/v/volleyball_court/outdoor 351
/w/waiting_room 352
/w/water_park 353
/w/water_tower 354
/w/waterfall 355
/w/watering_hole 356
/w/wave 357
/w/wet_bar 358
/w/wheat_field 359
/w/wind_farm 360
/w/windmill 361
/y/yard 362
/y/youth_hostel 363
/z/zen_garden 364

VGG16-places365 目标检测标出图片中的场景类别相关推荐

  1. 目标检测中,图片中object的坐标

    xml文件中显示object的位置信息与图片如何对应的 机器学习深度学习中在目标检测和识别时需要在训练模型时对大量的图片进行学习.在监督训炼中,需要对图进行label,通过软件或者脚本标注出图片中ob ...

  2. 下一步目标:整理出1套相对成熟的ios 开发框架

    下一步目标:整理出1套相对成熟的ios 开发框架 转载于:https://www.cnblogs.com/ygm900/archive/2013/05/28/3103422.html

  3. java图片去掉文字_java解出图片中的文字

    测试代码:public static String crackImage(String filePath) { File imageFile = new File(filePath); ITesser ...

  4. OpenCV找出图片中的圆并标注圆心

    1.概述 案例:输入一张图片找到图片中的一个圆,并标注圆心 实现步骤: 1.输入原图 2.图像图像灰度化 3.图像二值化 4.执行形态学操作去除噪点 5.边缘检测 6.轮廓发现 7.根据面积和纵横比过 ...

  5. python查找文字在图片中的位置_如何快速提取出图片中文字,强烈推荐这款免费软件!...

    作者 | 雷哥 编辑 | 小西瓜 工作中,难免会遇到需要提取图片中文本的情况.遇到这种场景,大家通常是怎么实现呢?手动输入吗? 还是通过软件进行提取文本呢? 1 手机 可以通过微信小程序-拍图识字进行 ...

  6. PS-图像处理:PS抠出图片中的图形轮廓渐变填充

    1.打开你要扣出轮廓线的图片素材 2.复制图层Ctrl+j 3.选中副本图层,去色,shift+Ctrl+u 4.滤镜--模糊--特殊模糊 5.在弹出的对话框中,设置它的模式为:仅限边缘,品质选择高, ...

  7. python 找出图片中的差异点,python opencv对目录下图片进行去重的技巧

    使用python opencv对目录下图片进行去重的方法 版本: 平台:ubuntu 14 / I5 / 4G内存 python版本:python2.7 opencv版本:2.13.4 依赖: 如果系 ...

  8. 利用yolov3 进行目标检测(可自定义检测的类别)

    最近在研究车辆检测相关算法,因yolo算法的速度和精度基本均能满足要求,原版的yolo v3算法是检测多类物体,本人是在原来代码的基础上修改为检测单目标车辆,并基于预训练权重在VOC2007数据集上进 ...

  9. 目标检测算法回顾之应用场景篇

    目标检测应用场景 目录 应用场景 总结 说明:本文仅供学习 目录 第一部分我们主要回答两个问题:目标检测在实际中有哪些应用以及为什么我们需要研究它的原因. 应用场景 在我们日常生活中有哪些可以想到的目 ...

最新文章

  1. iOS与JS交互的4种方法
  2. 【opencv】25.图像卷积cv::filter2D()以及c++代码实例
  3. c++对象的内存布局2--进阶篇---C++ 对象的内存布局(上)
  4. 博鳌直击 | 大数据开发的最大障碍是什么?
  5. 特殊的求和(函数和循环)
  6. java开发工程师学什么专业,Java核心知识点
  7. Android 系统(251)---sec2.0和sec2.1新增分区DA客制化步骤
  8. 经常玩电脑正确的坐姿_疼痛科专家告诫,疼痛是疾病的信号,经常腰腿疼,可能是身体在呼救...
  9. OpenDDS简单示例
  10. windows l2tp pptp设置
  11. 设计算法判断单链表的全部 n 个字符是否中心对称
  12. C语言实现FTP客户端(已编译,亲试可用~)
  13. mapreduce推测执行算法及原理
  14. 知乎爬虫请求头参数x-zse-96(代码可直接运行)
  15. 浏览器垃圾与回收机制
  16. 腾讯面试--测试工程师职位
  17. 【Shader进阶】Shader的Lod
  18. oralce启动问题You may need to set ORACLE_HOME to your Oracle software directory
  19. 程序人生 - 猫咪驱虫多久一次?
  20. 喜欢看的小说的可以收藏下

热门文章

  1. 吃豆人html代码原理,如何用HTML做一个吃豆人?
  2. 计算机毕业设计基于Android的在线相册管理系统
  3. 百练1002:方便记忆的电话号码(C++实现,北大软微)
  4. android模拟器不卡,安装Android模拟器——Genymotion
  5. 仿微信做的一个群组聊天头像的功能
  6. 财富杂志评出美国25大高薪公司
  7. Tomcat服务端口8080被占用解决方法
  8. c语言10分钟试讲,试讲10分钟讲点啥
  9. openlab搭建web网站
  10. 数据库插入两条一样数据解决方案