Cat and Dog Image Classifier
Cat and Dog Image Classifier
https://www.freecodecamp.org/learn/machine-learning-with-python/machine-learning-with-python-projects/cat-and-dog-image-classifier
For this challenge, you will use TensorFlow 2.0 and Keras to create a convolutional neural network that correctly classifies images of cats and dogs with at least 63% accuracy.
You can access the full project instructions and starter code on Google Colaboratory.
CNN解法
https://colab.research.google.com/drive/1mszXu-aNyHhEbJ9oi-52V3ZtNfwVq_vY#scrollTo=A9TMZH_oSULo
model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.Flatten()) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(10)) model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) history = model.fit(train_images, train_labels, epochs=4, validation_data=(test_images, test_labels))
Data Augmentation
To avoid overfitting and create a larger dataset from a smaller one we can use a technique called data augmentation. This is simply performing random transofrmations on our images so that our model can generalize better. These transformations can be things like compressions, rotations, stretches and even color changes.
Fortunately, keras can help us do this. Look at the code below to an example of data augmentation.
from keras.preprocessing import image from keras.preprocessing.image import ImageDataGenerator # creates a data generator object that transforms images datagen = ImageDataGenerator( rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest') # pick an image to transform test_img = train_images[20] img = image.img_to_array(test_img) # convert image to numpy arry img = img.reshape((1,) + img.shape) # reshape image i = 0 for batch in datagen.flow(img, save_prefix='test', save_format='jpeg'): # this loops runs forever until we break, saving images to current directory with specified prefix plt.figure(i) plot = plt.imshow(image.img_to_array(batch[0])) i += 1 if i > 4: # show 4 images break plt.show()
ImageDataGenerator
https://keras.io/zh/preprocessing/image/
keras.preprocessing.image.ImageDataGenerator(featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, zca_epsilon=1e-06, rotation_range=0, width_shift_range=0.0, height_shift_range=0.0, brightness_range=None, shear_range=0.0, zoom_range=0.0, channel_shift_range=0.0, fill_mode='nearest', cval=0.0, horizontal_flip=False, vertical_flip=False, rescale=None, preprocessing_function=None, data_format=None, validation_split=0.0, dtype=None)
通过实时数据增强生成张量图像数据批次。数据将不断循环(按批次)。
随机变换,增加样本多样性,避免过拟合。
(x_train, y_train), (x_test, y_test) = cifar10.load_data() y_train = np_utils.to_categorical(y_train, num_classes) y_test = np_utils.to_categorical(y_test, num_classes) datagen = ImageDataGenerator( featurewise_center=True, featurewise_std_normalization=True, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True) # 计算特征归一化所需的数量 # (如果应用 ZCA 白化,将计算标准差,均值,主成分) datagen.fit(x_train) # 使用实时数据增益的批数据对模型进行拟合: model.fit_generator(datagen.flow(x_train, y_train, batch_size=32), steps_per_epoch=len(x_train) / 32, epochs=epochs) # 这里有一个更 「手动」的例子 for e in range(epochs): print('Epoch', e) batches = 0 for x_batch, y_batch in datagen.flow(x_train, y_train, batch_size=32): model.fit(x_batch, y_batch) batches += 1 if batches >= len(x_train) / 32: # 我们需要手动打破循环, # 因为生成器会无限循环 break
对于海量图片,使用flow_from_directory 批量从磁盘上读取图片,节省内存空间。
train_datagen = ImageDataGenerator( rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory( 'data/train', target_size=(150, 150), batch_size=32, class_mode='binary') validation_generator = test_datagen.flow_from_directory( 'data/validation', target_size=(150, 150), batch_size=32, class_mode='binary') model.fit_generator( train_generator, steps_per_epoch=2000, epochs=50, validation_data=validation_generator, validation_steps=800)
image_dataset_from_directory
如果不需要进行随机变换,可以使用这个方法读取图片。
train_ds = tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split=0.2, subset="training", seed=123, image_size=(img_height, img_width), batch_size=batch_size)
使用这种方法,可以在模型层,做图片预处理,包括随机变换。
data_augmentation = keras.Sequential( [ layers.experimental.preprocessing.RandomFlip("horizontal", input_shape=(img_height, img_width, 3)), layers.experimental.preprocessing.RandomRotation(0.1), layers.experimental.preprocessing.RandomZoom(0.1), ] )
Keras模型结构
类似一个pipeline结构,可以设计不同的模型中的层,来处理不同的工作。
预测
https://www.tensorflow.org/tutorials/images/classification#predict_on_new_data
sunflower_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/592px-Red_sunflower.jpg" sunflower_path = tf.keras.utils.get_file('Red_sunflower', origin=sunflower_url) img = keras.preprocessing.image.load_img( sunflower_path, target_size=(img_height, img_width) ) img_array = keras.preprocessing.image.img_to_array(img) img_array = tf.expand_dims(img_array, 0) # Create a batch predictions = model.predict(img_array) score = tf.nn.softmax(predictions[0]) print( "This image most likely belongs to {} with a {:.2f} percent confidence." .format(class_names[np.argmax(score)], 100 * np.max(score)) )
Iris Prediction on Keras
https://gist.github.com/NiharG15/cd8272c9639941cf8f481a7c4478d525
""" A simple neural network written in Keras (TensorFlow backend) to classify the IRIS data """ import numpy as np from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import OneHotEncoder from keras.models import Sequential from keras.layers import Dense from keras.optimizers import Adam iris_data = load_iris() # load the iris dataset print('Example data: ') print(iris_data.data[:5]) print('Example labels: ') print(iris_data.target[:5]) x = iris_data.data y_ = iris_data.target.reshape(-1, 1) # Convert data to a single column # One Hot encode the class labels encoder = OneHotEncoder(sparse=False) y = encoder.fit_transform(y_) #print(y) # Split the data for training and testing train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.20) # Build the model model = Sequential() model.add(Dense(10, input_shape=(4,), activation='relu', name='fc1')) model.add(Dense(10, activation='relu', name='fc2')) model.add(Dense(3, activation='softmax', name='output')) # Adam optimizer with learning rate of 0.001 optimizer = Adam(lr=0.001) model.compile(optimizer, loss='categorical_crossentropy', metrics=['accuracy']) print('Neural Network Model Summary: ') print(model.summary()) # Train the model model.fit(train_x, train_y, verbose=2, batch_size=5, epochs=200) # Test on unseen data results = model.evaluate(test_x, test_y) print('Final test set loss: {:4f}'.format(results[0])) print('Final test set accuracy: {:4f}'.format(results[1]))
How to solve ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow`?
https://stackoverflow.com/questions/63006475/how-to-solve-importerror-keras-requires-tensorflow-2-2-or-higher-install-tenso
python -m pip install –upgrade pip
pip install keras==2.1.5
如何调整一个不收敛的卷积神经网络??
https://www.zhihu.com/question/36023110
这个帖子里讲的东西对我比较有用,分享出来。
My Neural Network isn't working! What should I do?
里面总结了11条可能出现的错误。简单翻译如下:
- 没有对数据做归一化。
- 没有检查过你的结果。这里的结果包括预处理结果和最终的训练测试结果。
- 忘了做数据预处理。
- 忘了使用正则化。
- Batch Size设的太大。
- 学习率设的不对。
- 最后一层的激活函数用的不对。
- 网络存在坏梯度。比如Relu对负值的梯度为0,反向传播时,0梯度就是不传播。
- 参数初始化错误。
- 网络太深。
- 隐藏层神经元数量错误。