【发布时间】:2020-05-02 23:31:42
【问题描述】:
我正在处理图像数据集
我首先调整了所有图像的大小
im_size1 = 128
im_size2 = 128
i = 0
for f, breed in tqdm(df_train.values):
if type(cv2.imread('train/{}.jpeg'.format(f)))==type(None):
continue
else:
img = cv2.imread('train/{}.jpeg'.format(f))
label = one_hot_labels[i]
x_train.append(cv2.resize(img, (im_size1, imt_size2)))
y_train.append(label)
i += 1
np.save('x_train2',x_train)
np.save('y_train2',y_train)
print('Done')
在第二次运行时,我通过了梯度特征直方图 注:两种情况下的型号相同。跑了不同的项目,得到了不同的结果
i = 0
for f, breed in tqdm(df_train.values):
if type(cv2.imread('train/{}.jpeg'.format(f)))==type(None):
continue
else:
img = cv2.imread('train/{}.jpeg'.format(f))
label = one_hot_labels[i]
resizedImage = cv2.resize(img, (im_size1, im_size2))
hog_vec, hog_vis = feature.hog(resizedImage, visualize=True)
resizedImageVec = cv2.resize(hog_vec, (im_size1, im_size2))
x_train.append(resizedImageVec)
#np.concatenate(x_train, hog_vec[:])
y_train.append(label)
i += 1
np.save('x_train2hog',x_train)
np.save('y_train2hog',y_train)
print('Done')
然后像这样配置模型
base_model = ResNet50(weights = None, include_top=False, input_shape=(im_size1, im_size2, 3))
# Add a new top layer
x = base_model.output
x = Flatten()(x)
x = Dropout(0.2)(x)
x = Dense(32, activation='relu')(x)
x = Dense(16, activation='relu')(x)
predictions = Dense(num_class, activation='softmax')(x)
# This is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)
# First: train only the top layers (which were randomly initialized)
#for layer in base_model.layers:
# layer.trainable = False
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
callbacks_list = [keras.callbacks.EarlyStopping(monitor='val_acc', verbose=1)]
model.summary()
现在我的教授问我这个问题。
“使用图像训练最后一层,然后使用特征(图像的不同表示)训练最后一层。将这两层连接起来,然后再训练一层。”
请指导我如何做到这一点
【问题讨论】:
-
这个问题可以改进和简化。例如,您所询问的内容并非特定于 ResNet
标签: python tensorflow keras deep-learning resnet