Classifying Images in Python

The model used will be of the sequential type, built using the TensorFlow library. Convolution layers (Conv2D), MaxPooling2D, Flatten, and Dense will be used, where the output layer will have a softmaxtype activation. # -*- coding: utf-8 -*- Automatically generated by Collaboratory. The original file is located at https://colab.research.google.com/drive/1PcmZhe-aF2W2uOFLYpCVnYMg6dmMXBNX # Import required Libraries """ importtensorflowastf fromtensorflowimportKeras importnumpyasnp importmatplotlib.pyplotasplt import copy fromkeras.utilsimportnp_utils importos fromosimportlistdir importpickle fromkeras.optimizersimport RMSprop importos importtarfile """# Define some model parameters""" TRAIN_SIZE =40000 local_zip='cifar-10-python.tar.gz' data_dir='cifar10' batches_dir=data_dir+'/cifar-10-batches-py' """# Define utils functions ### Function to load batches """ defload_train_batch(batches_dir, batch_id): file_name='data_batch_%i'%batch_id print("Loading batches from file "+file_name+"... ", end ="") f =open(os.path.join(batches_dir, file_name), mode ='rb') batch =pickle.load(f, encoding='latin1') images = batch['data'].reshape((len(batch['data']),3,32, 32)).transpose(0, 2, 3, 1) labels = batch['labels'] print("done") return images, labels defload_test_batch(batches_dir): f =open(os.path.join(batches_dir, 'test_batch'), mode ='rb') batch =pickle.load(f, encoding='latin1') images = batch['data'].reshape((len(batch['data']),3,32, 32)).transpose(0, 2, 3, 1) labels = batch['labels'] return images, labels """### Function to normalize images""" defrescale(im): returnim/255.0 """### Function to load all batches""" defload_train_batches(batches_dir): images = [] labels = [] # Get how many batches are inside the file and read each of them files = [f for f inlistdir(batches_dir)] n =1 total_samples=0 for f in files: if'data_batch_'in f: batch_images, batch_labels=load_train_batch(batches_dir, n) N =len(batch_labels) total_samples+= N batch_labels=np.array(batch_labels) labels.append(batch_labels) images.append(batch_images) n +=1 returnnp.array(images).reshape(total_samples, 32, 32, 3), np.array(labels).reshape(total_samples, 1) defload_test_batches(batches_dir): images = [] labels = [] # Get how many batches are inside the file and read each of them files = [f for f inlistdir(batches_dir)] n =1 total_samples=0 batch_images, batch_labels=load_test_batch(batches_dir) N =len(batch_labels) total_samples+= N batch_labels=np.array(batch_labels) labels.append(batch_labels) images.append(batch_images) n +=1 returnnp.array(images).reshape(total_samples, 32, 32, 3), np.array(labels).reshape(total_samples, 1) """### Function to plot a grid of images""" defplot_images(size_x, size_y, images, labels_id, labels_str): """ size_x: Number of rows in the grid size_y: Number of columns in the grid """ print("") print("Creating grid of {}x{} images... ".format(size_x, size_y), end="") N =images.shape[0] fig, axs=plt.subplots(size_x, size_y, figsize= (30,30)) foriinrange(size_x): for j inrange(size_y): k =np.random.randint(N-1,size=1)[0] label =labels_str[labels_id[k][0]] axs[i,j].imshow(images[k]) axs[i,j].set_title(label) print("done") """### Function to plot metrics from model, like accuracy, loss, etc""" defplot_metrics(history, metrics_list): for metric inmetrics_list: plt.plot(history.history[metric], label = metric) plt.grid(True) plt.legend() plt.xlabel('Epochs') plt.ylabel('Value') plt.show() """## Custom Loss Function""" defcustom_loss(ytrue, ypred): ytrue=tf.dtypes.cast(ytrue, tf.float32) ypred=tf.dtypes.cast(ypred, tf.float32) returntf.reduce_mean(ytrue-ypred) """# Functions to Load (or Download) the data""" """ def load_data_colab(): !wget --no-check-certificate \ https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz \ -O /tmp/cifar-10-python.tar.gz tarf = tarfile.open(local_zip, 'r') tarf.extractall(data_dir) """ defload_data(): tarf=tarfile.open(local_zip, 'r') tarf.extractall(data_dir) """# Downlad Data and extract it into folder""" load_data() """# Load Batches and save into NumPy Arrays""" x, y =load_train_batches(batches_dir) x_test, y_test=load_test_batches(batches_dir) """# Split into Training, Validation, and Test ## We will use 40000 for training, 10000 for validation, and 10000 for testing """ x_train= x[:TRAIN_SIZE] y_train= y[:TRAIN_SIZE] x_valid= x[TRAIN_SIZE:] y_valid= y[TRAIN_SIZE:] N_train=x_train.shape[0] N_valid=x_valid.shape[0] N_test=x_test.shape[0] labels = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] print("There are {} training images, {} validation images and {} testing images".format(N_train, N_valid, N_test)) """# Create a Grid of 10x10 and display 100 random images""" plot_images(10,10, x_train, y_train, labels) """# Prepare for training, validation and testing ### Re-scale Images from 0 to 1 """ x_train= rescale(x_train) x_test= rescale(x_test) x_valid=rescale(x_valid) """# Create model""" model =tf.keras.Sequential([ tf.keras.layers.Conv2D(32, (3,3), input_shape= (32, 32, 3), activation ='relu'), tf.keras.layers.MaxPooling2D((2,2)), tf.keras.layers.Flatten(), tf.keras.layers.Dense(512, activation='relu'), tf.keras.layers.Dense(10, activation ='softmax') ]) model.compile(loss ='sparse_categorical_crossentropy', optimizer ='sgd', metrics = ['accuracy']) model.summary() print("") print("Training model with loss = 'sparse_categorical_crossentropy' and optimizer = 'sgd'... ") history =model.fit(x_train, y_train, validation_data= (x_valid, y_valid), epochs =15, verbose =1) """# Now, plot accuracy and loss""" plot_metrics(history, ['accuracy', 'val_accuracy']) plot_metrics(history, ['loss', 'val_loss']) """# Evaluate the model""" test_loss, test_acc=model.evaluate(x_test, y_test, verbose=2) """# Now, we will train a new model, but this time using our custom loss function""" model.compile(loss =custom_loss, optimizer ='adam', metrics = ['accuracy', custom_loss]) print("Training model with loss = 'custom_loss' and optimizer = 'sgd'... ") history2 =model.fit(x_train, y_train, validation_data= (x_valid, y_valid), epochs =15) """# Evaluate model""" ret =model.evaluate(x_test, y_test, verbose=2) print("Loss: {}\nAccuracy: {}\nCustom Loss: {}".format(ret[0], ret[1], ret[2]))