Tang teacher of cats and dogs from project identification and project data set.
Item specific implementation steps:
1. Read the training data set 500 cats +500.
2. The processing of the picture read, processed into a uniform size format, divided into many labels.
3.shuffle about the cats and dogs doping mixed data, random as possible.
4. Using CNN network training test.
Specific code as follows:
1. Read the training set.
import pandas as pd import numpy as np import os import glob import matplotlib.pyplot as plt import cv2 as cv2
images = [] labels = [] img_names = [] cls = [] train_path="training_data" classes = ['dogs','cats'] num_classes = len(classes) image_size=128 print('Going to read training images') for fields in classes: index = classes.index(fields) print('Now going to read {} files (Index: {})'.format(fields, index)) path = os.path.join(train_path, fields, '*g') files = glob.glob(path) print(len(files)) for fl in files: image = cv2.imread(fl) image = cv2.resize(image, (image_size, image_size),0,0, cv2.INTER_LINEAR) image = image.astype(np.float32) image = np.multiply(image, 1.0 / 255.0) images.append(image) label = np.zeros(len(classes)) label[index] = 1.0 labels.append(label) flbase = os.path.basename(fl) img_names.append(flbase) cls.append(fields) images = np.array(images) labels = np.array(labels) img_names = np.array(img_names) cls = np.array(cls)
2. The training data set.
from tensorflow import keras from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPool2D from tensorflow.keras import Input from sklearn.model_selection import train_test_split from sklearn.utils import shuffle from tensorflow.keras.regularizers import l1
# Dataset processor Images, Labels = shuffle (Images, Labels) X_train, X_test, y_train, android.permission.FACTOR. = Train_test_split (Images, Labels)
= Model the Sequential () # first layer convolution, the convolution kernel 32, the size of 5x5, convolution mode SAME, RELU activation function, the magnitude of the input tensor model.add (Conv2D (filters = 6, kernel_size = (3 ,. 3), padding = ' Valid ' , kernel_regularizer L1 = (0.1), Activation = ' tanh ' , input_shape = (128,128,3 ))) # model.add (Conv2D (= Filters 32, kernel_size = (3,3) , padding = 'Valid', Activation = 'RELU')) # pooled layer, pooling nuclear size 2x2 model.add (MaxPool2D (pool_size = (2,2 & ))) # random drop quarter of network connections, to prevent overfitting model.add (Dropout (0.5 )) model.add (Conv2D (Filters =. 6, kernel_size = (3,3), padding = ' Same, ' , = Activation' Tanh ' )) # model.add (Conv2D (= Filters. 6, kernel_size = (3,3), padding = 'Same,', Activation = 'tanh')) model.add (MaxPool2D (pool_size = (2,2 &) , Strides = (2,2 & ))) model.add (Dropout ( 0.5 )) # fully connected layers, unfolding operation, model.add (the Flatten ()) # add a hidden layer neuron activation function and number # model.add (the Dense (120, Activation = 'tanh')) model.add (Dropout (0.5 )) # model.add (the Dense (84, Activation = 'tanh')) # output layer model.add (Dense (2, activation = ' SoftMax ' ))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(images,labels,validation_split=0.2,batch_size=128,epochs=50,c)
# Model storage MP = " model_3_1625.h5 " model.save (MP)
# Model Evaluation model.evaluate (X_test, y_test)
Training process feedback is as follows: