我在此链接https://github.com/llSourcell/how_to_make_an_image_classifier/blob/master/demo.ipynb中使用了此代码示例
我在第一次跑步时就获得了准确性,我接受“ 0.70278823” 这是我在colab上运行的代码,但其数据集存在于链接中
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dropout, Flatten, Dense
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D
from tensorflow.keras import optimizers
from google.colab import drive
drive.mount('/content/drive')
img_width, img_height = 150, 150
train_data_dir =( "/content/drive/My Drive/sample/train")
validation_data_dir = ("/content/drive/My Drive/sample/valid")
# used to rescale the pixel values from [0, 255] to [0, 1] interval
datagen = ImageDataGenerator(rescale=1./255)
# automagically retrieve images and their classes for train and validation sets
train_generator = datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=8,
class_mode='binary')
validation_generator = datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=8,
class_mode='binary')
model = Sequential()
model.add(Convolution2D(32, (3, 3), input_shape=(img_width, img_height,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(64,( 3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
# lose function that defind accuracy
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
nb_epochs = 30
nb_train_samples = 2048
nb_validation_samples = 832
history=model.fit_generator(train_generator,
steps_per_epoch=nb_train_samples,
epochs=nb_epochs
, validation_data=validation_generator
, validation_steps=nb_validation_samples)
model.save_weights('basic_cnn_20_epochs.h5')
print(model.evaluate_generator(validation_generator, nb_validation_samples))
我的问题是,如示例所示,当我尝试提高自己的准确性时,我得到的准确性较低,而不是更好。 我只是尝试在新的单元格中运行此代码,而我得到的总是比获得的收益低。
train_datagen_augmented = ImageDataGenerator(
rescale=1./255, # normalize pixel values to [0,1]\n",
shear_range=0.2, # randomly applies shearing transformation\n",
zoom_range=0.2, # randomly applies shearing transformation\n",
horizontal_flip=True) # randomly flip the images
train_generator_augmented = train_datagen_augmented.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=8,
class_mode='binary')
model.fit_generator(
train_generator_augmented,
steps_per_epoch=nb_train_samples,
epochs=nb_epochs
, validation_data=validation_generator
, validation_steps=nb_validation_samples)
print(model.evaluate_generator(validation_generator, nb_validation_samples))
此后,如果我将批次降低到8,则我的准确度是0.6和0.5。那么为什么我的精度下降呢?我正在使用图像翻转,所以我可以给出相同的数据,但是在翻转模式下,将新数据放入模型中应该可以提高模型精度,即使这样一点也不降低。