Demo entry 6783504

python

   

Submitted by anonymous on Feb 14, 2019 at 00:51
Language: Python. Code size: 6.0 kB.

'''Import modules'''

import glob
import os.path
import random
import math

import cv2
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers import BatchNormalization, GlobalMaxPooling2D, GlobalAveragePooling2D
from keras.optimizers import SGD, Adam, RMSprop
from keras import Model
from keras.datasets import cifar10
from keras.utils import np_utils
from keras.models import Sequential
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg16 import VGG16
from keras.applications.mobilenet import MobileNet
import matplotlib.pyplot as plt
import numpy as np



def data_loader(name, normalize=True, categorical=True, NB_CLASSES=10,  resize=False, res_shape=(128, 128)):
    '''
    Funtion for loading the input dataset:
    Input:
            name: name of the dataset
            normalize: boolean variable for normalsing the dataset
            categorical: boolean variable for converting the lables to categorical
            NB_CLASSES: number of classes in the input dataset
            resize: boolean variable for resizing the images of the input dataset to any size
            res_shape: a tuple with the desired resizing shape

    Output: a tuple with the trainning and testing images and lables
    '''
    if(name=='CIFAR10'):
        (X_train, y_train), (X_test, y_test) = cifar10.load_data()
        print(X_train.shape, 'training samples')
        print(X_test.shape, 'test samples')

    else:
        # you can add your own dataset here
        raise ValueError('Not supported dataset')

    # Normalization step
    if (normalize):
        X_train = X_train.astype('float32')
        X_test = X_test.astype('float32')
        X_train /= 255
        X_test /= 255

    # convert to categorical
    if (categorical):
        Y_train = np_utils.to_categorical(y_train, NB_CLASSES)
        Y_test = np_utils.to_categorical(y_test, NB_CLASSES)

    # Resize trainging images
    if(resize):
         img_rows= res_shape[0]
         img_cols= res_shape[1]
         X_train = np.array([cv2.resize(img, (img_rows,img_cols)) for img in X_train])
         X_test = np.array([cv2.resize(img, (img_rows,img_cols)) for img in X_test])

    return (X_train, Y_train, X_test, Y_test)


def define_model(name='CNN_baseline', model_opt={}):
    '''
    Funtion for defining the CNN architecture:
    Input:
            name: name of the model, helpful when you have more than one architecture to choose from
            model_opt: a dictionary with the hyperparameters required to build a model

    Output: the built model
    '''
    if(name=='CNN_baseline'):
        # network architecture
        print('>>>>>>>> model of choice is: %s >>>>>>>>' %('CNN_baseline'))

        # load model parameters
        IMG_CHANNELS = model_opt.get('IMG_CHANNELS', 3)
        IMG_ROWS = model_opt.get('IMG_ROWS', 32)
        IMG_COLS = model_opt.get('IMG_COLS', 32)
        bn_axis = model_opt.get('bn_axis', 3)
        NB_CLASSES = model_opt.get('NB_CLASSES', 10)

        # build the baseline network model
        model = Sequential()
        model.add(Conv2D(32, (3, 3), padding='same', input_shape=(IMG_ROWS, IMG_COLS, IMG_CHANNELS)))
        model.add(BatchNormalization(axis=bn_axis))
        model.add(Activation('relu'))

        model.add(Flatten())
        model.add(Dense(512))
        model.add(Activation('relu'))
        model.add(Dropout(0.5))

        model.add(Dense(NB_CLASSES))
        model.add(Activation('softmax'))

        # print summary of the designed model
        model.summary()

        return model

    else:
        # you can add any architecture here
        raise ValueError('Not supported architecture')


def train_model(X_train, Y_train, model, optimizer_opt={}, fit_opt={}):
    '''
    Funtion for training the CNN model:
    Input:
            X_train: training dataset images
            Y_train: training dataset classes/labels
            model: Keras model
            optimizer_opt: a dictionary with optimization parameters
            fit_opt: a dictionary with fitting parameters

    Output: the trained model
    '''
    OPT = optimizer_opt.get('OPT', 'Adam')
    loss = optimizer_opt.get('loss', 'categorical_crossentropy')
    metrics = optimizer_opt.get('metrics', ['accuracy'])
    just_compile = optimizer_opt.get('just_compile', False)
    if(just_compile):
        model.compile(optimizer=OPT, loss=loss,  metrics=metrics)
        return model

    model.compile(optimizer=OPT, loss=loss,  metrics=metrics)
    BATCH_SIZE = fit_opt.get('BATCH_SIZE', 128)
    NB_EPOCH = fit_opt.get('NB_EPOCH', 40)
    VERBOSE = fit_opt.get('VERBOSE', 1)

    # train the model
    model.fit(X_train, Y_train, batch_size=BATCH_SIZE, epochs=NB_EPOCH, verbose=1)

    # save the trained model (architecture, weights and training configuration)
    model.save('CNN_baseline.h5')  # creates an HDF5 file 'CNN_baseline.h5'

    return model

def eval_model(X_test, Y_test, model, eval_opt={}):
    '''
    Funtion for evaluating the CNN trained model:
    Input:
            X_test: test dataset images
            Y_test: test dataset classes/labels
            model: Keras model
            eval_opt: a dictionary with evaluation parameters

    Output: the result of the evaluation as a score/accuracy
    '''
    BATCH_SIZE = eval_opt.get('BATCH_SIZE', 128)
    VERBOSE = eval_opt.get('VERBOSE', 1)
    score = model.evaluate(X_test, Y_test, batch_size=BATCH_SIZE, verbose=VERBOSE)
    print('Test accuracy:', score[1])
    return score


if __name__=='__main__':
    (X_train, Y_train, X_test, Y_test) = data_loader('CIFAR10')
    model = define_model(name='CNN_baseline')
    model = train_model(X_train, Y_train, model, fit_opt={'NB_EPOCH':1})

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).