Classify dogs and cats on svm algorithm
import pandas as pd import numpy as np import tensorflow as tf
import warnings warnings.filterwarnings('ignore')
from tensorflow.compat.v1 import ConfigProto from tensorflow.compat.v1 import InteractiveSession
config = ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.6 config.gpu_options.allow_growth = True session = InteractiveSession(config=config)
datagen1 = ImageDataGenerator(rescale=1./255)
test_set = datagen1.flow_from_directory( "../input/cat-and-dog/test_set/test_set", target_size=(64, 64), batch_size=32, class_mode="binary" )
from tensorflow.keras.layers import Conv2D from tensorflow.keras.layers import Dense from tensorflow.keras.regularizers import l2 cnn = tf.keras.models.Sequential() cnn = tf.keras.models.Sequential()cnn.add(tf.keras.layers.Conv2D(filters=32,padding='same',kernel_size=3, activation='relu')) cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2)) cnn.add(tf.keras.layers.Flatten())cnn.add(Dense(1, kernel_regularizer=tf.keras.regularizers.l2(0.01),activation ='linear')) cnn.summary()
cnn.compile(optimizer = 'adam', loss = 'hinge', metrics = ['accuracy']) r=cnn.fit(x = training_set, validation_data = test_set, epochs = 15)
import matplotlib.pyplot as plt plt.plot(r.history['loss'], label='train loss') plt.plot(r.history['val_loss'], label='val loss') plt.legend() plt.show()
plt.plot(r.history['accuracy'], label='train acc') plt.plot(r.history['val_accuracy'], label='val acc') plt.legend() plt.show()from tensorflow.keras.models import load_model
cnn.save('./model_rcat_dog.h5') from tensorflow.keras.preprocessing import image test_image = image.load_img('../input/cat-and-dog/training_set/training_set/dogs/dog.1023.jpg', target_size = (64,64)) test_image = image.img_to_array(test_image) test_image=test_image/255 test_image = np.expand_dims(test_image, axis = 0) result = cnn.predict(test_image) if result[0]<0: print("The image classified is cat") else: print("The image classified is dog")if result[0]<0: print("The image classified is cat") else: print("The image classified is dog")