<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Usage of Keras for image_classification
configure random transformations and normalization operations to be done on your image data during training
instantiate generators of augmented image batches (and their labels) via .flow(data, labels) or .flow_from_directory(directory). These generators can then be used with the Keras model methods that accept data generators as inputs, fit_generator, evaluate_generator and predict_generator.
Step1:  GPU configuration, disregard if non-GPU
Step2:  inspirational architectures taken from
brain_segmentation
image_classification
Step3:  Testing to predict to see outcome of the predictions
<ASSISTANT_TASK:>
Python Code:
import os
curr_dir = %pwd
proj_dir = os.path.normpath(os.path.join(curr_dir, os.path.pardir))
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import regularizers
from keras.layers.normalization import BatchNormalization
import numpy as np
seed = 13
np.random.seed(seed)
Explanation: Usage of Keras for image_classification
configure random transformations and normalization operations to be done on your image data during training
instantiate generators of augmented image batches (and their labels) via .flow(data, labels) or .flow_from_directory(directory). These generators can then be used with the Keras model methods that accept data generators as inputs, fit_generator, evaluate_generator and predict_generator.
End of explanation
## use only 30 % of the memory
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5
set_session(tf.Session(config=config))
# imports to display the svg image of the network
from keras.utils import plot_model
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
import matplotlib.pyplot as plt
import pydot
import graphviz
from keras.preprocessing.image import ImageDataGenerator
batch_size = 32
# train_datagen = ImageDataGenerator(rescale=1./255)
train_datagen = ImageDataGenerator(rescale=1./255, horizontal_flip=True, width_shift_range=0.1,
                                  height_shift_range=0.1,zoom_range=0.1)
# only rescaling augmentation for test samples
test_datagen = ImageDataGenerator(rescale=1./255)
train_dir = '../data/processed/train'
test_dir = '../data/processed/test'
%pwd
# this is a generaotr that will read pictures 
# found in sub folders of 'data/train',
# and indefinitely generate batches of augmented image data
train_generator = train_datagen.flow_from_directory(
                train_dir,
                target_size=(100,50),
                shuffle=True,
                color_mode='grayscale',
                batch_size=batch_size,
                class_mode='categorical') # since we use categoricalentropy loss
# this is a similar generatr, for validation/test data
test_generator = test_datagen.flow_from_directory(
    test_dir,
    target_size=(100,50),
    color_mode='grayscale',
    batch_size=batch_size,
    class_mode='categorical')
Explanation: GPU configuration, disregard if non-GPU
End of explanation
nb_train_samples = 400
nb_test_samples = 100
model = Sequential()
model.add(Conv2D(64, (3,3), input_shape=(100,50,1), padding='same',
                 kernel_regularizer=regularizers.l2(0.001),activity_regularizer=regularizers.l1(0.001)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))
model.add(Conv2D(128, (3,3), padding='same'))#, kernel_regularizer=regularizers.l2(0.01),activity_regularizer=regularizers.l1(0.01)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(5))
model.add(Activation('softmax'))
from keras.optimizers import Adam
          
adam = Adam(lr=0.0001)
model.compile(loss='categorical_crossentropy',
              optimizer=adam,
              metrics=['accuracy'])
SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))
model.summary()
run_count = 0
run_count += 1
from keras.callbacks import TensorBoard
tbCallBack = TensorBoard(log_dir="./summary/run_{}".format(run_count), 
                         histogram_freq=2, write_graph=True, 
                         write_images=True)
model.fit_generator(train_generator,
                   steps_per_epoch=nb_train_samples // batch_size,
                   epochs=120,
                   validation_data=test_generator,
                   validation_steps=nb_test_samples // batch_size,
                   callbacks=[tbCallBack])
model.save_weights('first_try.h5')
model.save('my_model.h5')
Explanation: inspirational architectures taken from
brain_segmentation
image_classification
End of explanation
import random
test_data_path = "data/processed/test/Body"
random_image = random.choice(os.listdir(test_filepath))
test__image_filepath = os.path.normpath(os.path.join(proj_dir, test_data_path, random_image))
import scipy.misc
proj_dir = os.path.curdir
image = scipy.misc.imread(test_image_filepath,mode='L')
image = scipy.misc.imresize(image, (100,50))
image = image / 255
image = np.expand_dims(image, axis=2)
model.save('../models/80_mf.h5')
image = image.reshape((1,) + image.shape)
model.predict_proba(image, verbose=1, batch_size=1)
model.predict(image)
train_generator.class_indices
Explanation: Testing to predict to see outcome of the predictions
End of explanation
<END_TASK>