我目前正在使用VGG16训练oxford-3T PETS数据集。我正在使用keras,但是损失和准确性不会随时间变化。我在具有相同数据排列的先前food101数据集上使用了此代码,并且效果很好。我完全不知道。
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib.image as img
from keras.models import Sequential
from keras.layers import Flatten, Dense, Input, Lambda
from keras.layers.convolutional import Conv2D, MaxPooling2D, ZeroPadding2D
from keras.utils import to_categorical, multi_gpu_model
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from keras import optimizers
from keras.applications.vgg16 import VGG16, preprocess_input
from keras.models import Model
import os
import keras.backend.tensorflow_backend as KTF
import numpy as np
import cv2
from PIL import Image
import pickle
os.environ["CUDA_VISIBLE_DEVICES"]="0"
#parameters
batch_size = 32
n_epoch = 50
lr_rate = 0.001
#data augementation
train_datagen = ImageDataGenerator(rescale = 1./255,
rotation_range = 30,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
with tf.device('/cpu:0'):
# load data
train_generator = train_datagen.flow_from_directory('/home/boxuanwu/Pictures/oxfordpet/images/train',
target_size=(224,224),
color_mode='rgb',
batch_size=batch_size,
class_mode='categorical',shuffle=True)
test_generator = test_datagen.flow_from_directory('/home/boxuanwu/Pictures/oxfordpet/images/test',
target_size=(224,224),
color_mode='rgb',
batch_size=batch_size,
class_mode='categorical',shuffle=True)
#initialize the model
model = Sequential()
# Conv Block 1
model.add(Conv2D(64, (3, 3), input_shape=(224,224,3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# Conv Block 2
model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# Conv Block 3
model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# Conv Block 4
model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# Conv Block 5
model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# FC layers
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dense(4096, activation='relu'))
model.add(Dense(37, activation='softmax'))
network = model
#network = multi_gpu_model(model, 2)
#sgd = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=None, decay=0.0)
sgd = optimizers.SGD(lr=lr_rate, decay=1e-6, momentum=0.9, nesterov=True)
network.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
network.summary()
# train
history_callback = network.fit_generator(generator=train_generator,
steps_per_epoch=50,
epochs=n_epoch,
validation_data=test_generator,
validation_steps=10, verbose=2)
#use_multiprocessing=False)
这是前几个时期的日志。它保持在3%左右,这几乎是随机猜测。预期结果应该在60-70%左右,我的朋友在pyTorch上使用完全相同的网络结构和数据对其进行了测试。
- 28s - loss: 3.6109 - acc: 0.0281 - val_loss: 3.6109 - val_acc: 0.0219
Epoch 2/50
- 24s - loss: 3.6111 - acc: 0.0238 - val_loss: 3.6110 - val_acc: 0.0375
Epoch 3/50
- 24s - loss: 3.6114 - acc: 0.0250 - val_loss: 3.6108 - val_acc: 0.0281
Epoch 4/50
- 24s - loss: 3.6112 - acc: 0.0181 - val_loss: 3.6106 - val_acc: 0.0531
Epoch 5/50
- 25s - loss: 3.6109 - acc: 0.0312 - val_loss: 3.6108 - val_acc: 0.0305
Epoch 6/50
- 23s - loss: 3.6109 - acc: 0.0181 - val_loss: 3.6105 - val_acc: 0.0406
Epoch 7/50
- 23s - loss: 3.6113 - acc: 0.0256 - val_loss: 3.6110 - val_acc: 0.0219
Epoch 8/50
- 23s - loss: 3.6108 - acc: 0.0309 - val_loss: 3.6106 - val_acc: 0.0375
Epoch 9/50
- 24s - loss: 3.6107 - acc: 0.0312 - val_loss: 3.6104 - val_acc: 0.0156
Epoch 10/50
- 24s - loss: 3.6111 - acc: 0.0269 - val_loss: 3.6102 - val_acc: 0.0475
Epoch 11/50
- 24s - loss: 3.6112 - acc: 0.0231 - val_loss: 3.6103 - val_acc: 0.0500
Epoch 12/50
- 24s - loss: 3.6107 - acc: 0.0290 - val_loss: 3.6103 - val_acc: 0.0250
Epoch 13/50
- 24s - loss: 3.6108 - acc: 0.0294 - val_loss: 3.6106 - val_acc: 0.0406
Epoch 14/50
- 25s - loss: 3.6109 - acc: 0.0294 - val_loss: 3.6106 - val_acc: 0.0594
Epoch 15/50
- 24s - loss: 3.6109 - acc: 0.0363 - val_loss: 3.6105 - val_acc: 0.0576
Epoch 16/50
- 24s - loss: 3.6104 - acc: 0.0400 - val_loss: 3.6104 - val_acc: 0.0312
Epoch 17/50
- 24s - loss: 3.6105 - acc: 0.0300 - val_loss: 3.6105 - val_acc: 0.0500
Epoch 18/50
- 24s - loss: 3.6111 - acc: 0.0325 - val_loss: 3.6102 - val_acc: 0.0500
Epoch 19/50
- 23s - loss: 3.6107 - acc: 0.0384 - val_loss: 3.6099 - val_acc: 0.0542
Epoch 20/50
- 23s - loss: 3.6105 - acc: 0.0350 - val_loss: 3.6097 - val_acc: 0.0281