<jupyter_start><jupyter_text><jupyter_code># https://keras.io/
!pip install -q keras
import keras

import keras
from keras.datasets import cifar10
from keras.models import Model, Sequential
from keras.layers import Dense, Dropout, Flatten, Input, AveragePooling2D, merge, Activation
from keras.layers import Conv2D, MaxPooling2D, BatchNormalization
from keras.layers import Concatenate
from keras.optimizers import Adam
from keras.layers.advanced_activations import LeakyReLU
from keras.layers import Reshape, Activation, Conv2D, Input, MaxPooling2D, BatchNormalization, Flatten, Dense, Lambda
from keras.layers.merge import concatenate

# this part will prevent tensorflow to allocate all the avaliable GPU Memory
# backend
import tensorflow as tf
from keras import backend as k

# Don't pre-allocate memory; allocate as-needed
config = tf.ConfigProto()
config.gpu_options.allow_growth = True

# Create a session with the above options specified.
k.tensorflow_backend.set_session(tf.Session(config=config))

# Hyperparameters
batch_size = 128
num_classes = 10
epochs = 50
l = 10
num_filter = 20
# Load CIFAR10 Data
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
img_height, img_width, channel = x_train.shape[1],x_train.shape[2],x_train.shape[3]

# convert to one hot encoding 
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
def space_to_depth_x2(x):
    return tf.space_to_depth(x, block_size=2)
input = Input(shape=(img_height, img_width, channel,))

# Layer 1
x01 = Conv2D(32, (3,3), strides=(1,1), padding='same', name='conv_1', use_bias=False)(input)
x01 = BatchNormalization(name='norm_1')(x01)
x01 = LeakyReLU(alpha=0.1)(x01)
x01 = MaxPooling2D(pool_size=(2, 2))(x01)

# Layer 2
x02 = Conv2D(64, (3,3), strides=(1,1), padding='same', name='conv_2', use_bias=False)(x01)
x02 = BatchNormalization(name='norm_2')(x02)
x02 = LeakyReLU(alpha=0.1)(x02)
x02 = MaxPooling2D(pool_size=(2, 2))(x02)

# Layer 3
x03 = Conv2D(128, (3,3), strides=(1,1), padding='same', name='conv_3', use_bias=False)(x02)
x03 = BatchNormalization(name='norm_3')(x03)
x03 = LeakyReLU(alpha=0.1)(x03)

# Layer 4
x04 = Conv2D(64, (1,1), strides=(1,1), padding='same', name='conv_4', use_bias=False)(x03)
x04 = BatchNormalization(name='norm_4')(x04)
x04 = LeakyReLU(alpha=0.1)(x04)

# Layer 5
x05 = Conv2D(128, (3,3), strides=(1,1), padding='same', name='conv_5', use_bias=False)(x04)
x05 = BatchNormalization(name='norm_5')(x05)
x05 = LeakyReLU(alpha=0.1)(x05)
x05 = MaxPooling2D(pool_size=(2, 2))(x05)

# Layer 6
x06 = Conv2D(256, (3,3), strides=(1,1), padding='same', name='conv_6', use_bias=False)(x05)
x06 = BatchNormalization(name='norm_6')(x06)
x06 = LeakyReLU(alpha=0.1)(x06)

# Layer 7
x07 = Conv2D(128, (1,1), strides=(1,1), padding='same', name='conv_7', use_bias=False)(x06)
x07 = BatchNormalization(name='norm_7')(x07)
x07 = LeakyReLU(alpha=0.1)(x07)

# Layer 8
x08 = Conv2D(256, (3,3), strides=(1,1), padding='same', name='conv_8', use_bias=False)(x07)
x08 = BatchNormalization(name='norm_8')(x08)
x08 = LeakyReLU(alpha=0.1)(x08)
x08 = MaxPooling2D(pool_size=(2, 2))(x08)

# Layer 9
x09 = Conv2D(512, (3,3), strides=(1,1), padding='same', name='conv_9', use_bias=False)(x08)
x09 = BatchNormalization(name='norm_9')(x09)
x09 = LeakyReLU(alpha=0.1)(x09)

# Layer 10
x10 = Conv2D(256, (1,1), strides=(1,1), padding='same', name='conv_10', use_bias=False)(x09)
x10 = BatchNormalization(name='norm_10')(x10)
x10 = LeakyReLU(alpha=0.1)(x10)

# Layer 11
x11 = Conv2D(512, (3,3), strides=(1,1), padding='same', name='conv_11', use_bias=False)(x10)
x11 = BatchNormalization(name='norm_11')(x11)
x11 = LeakyReLU(alpha=0.1)(x11)

# Layer 12
x12 = Conv2D(256, (1,1), strides=(1,1), padding='same', name='conv_12', use_bias=False)(x11)
x12 = BatchNormalization(name='norm_12')(x12)
x12 = LeakyReLU(alpha=0.1)(x12)

skip_connection = x12

# Layer 13
x13 = Conv2D(512, (3,3), strides=(1,1), padding='same', name='conv_13', use_bias=False)(x12)
x13 = BatchNormalization(name='norm_13')(x13)
x13 = LeakyReLU(alpha=0.1)(x13)

#skip_connection = x

x13 = MaxPooling2D(pool_size=(2, 2))(x13)

# Layer 14
x14 = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_14', use_bias=False)(x13)
x14 = BatchNormalization(name='norm_14')(x14)
x14 = LeakyReLU(alpha=0.1)(x14)

# Layer 15
x15 = Conv2D(512, (1,1), strides=(1,1), padding='same', name='conv_15', use_bias=False)(x14)
x15 = BatchNormalization(name='norm_15')(x15)
x15 = LeakyReLU(alpha=0.1)(x15)

# Layer 16
x16 = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_16', use_bias=False)(x15)
x16 = BatchNormalization(name='norm_16')(x16)
x16 = LeakyReLU(alpha=0.1)(x16)

# Layer 17
x17 = Conv2D(512, (1,1), strides=(1,1), padding='same', name='conv_17', use_bias=False)(x16)
x17 = BatchNormalization(name='norm_17')(x17)
x17 = LeakyReLU(alpha=0.1)(x17)

# Layer 18
x18 = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_18', use_bias=False)(x17)
x18 = BatchNormalization(name='norm_18')(x18)
x18 = LeakyReLU(alpha=0.1)(x18)

# Layer 19
x19 = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_19', use_bias=False)(x18)
x19 = BatchNormalization(name='norm_19')(x19)
x19 = LeakyReLU(alpha=0.1)(x19)

# Layer 20
x20 = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_20', use_bias=False)(x19)
x20 = BatchNormalization(name='norm_20')(x20)
x20 = LeakyReLU(alpha=0.1)(x20)

# Layer 21
skip_connection = Conv2D(64, (1,1), strides=(1,1), padding='same', name='conv_21', use_bias=False)(skip_connection)
skip_connection = BatchNormalization(name='norm_21')(skip_connection)
skip_connection = LeakyReLU(alpha=0.1)(skip_connection)
skip_connection = Lambda(space_to_depth_x2)(skip_connection)

x21 = concatenate([skip_connection, x20])

# Layer 22
x22 = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_22', use_bias=False)(x21)
x22 = BatchNormalization(name='norm_22')(x22)
x22 = LeakyReLU(alpha=0.1)(x22)

# Layer 23
x23 = Flatten()(x22)

output = Dense(num_classes, activation='softmax')(x23)


model = Model(inputs=[input], outputs=[output])
model.summary()
# determine Loss function and Optimizer
model.compile(loss='categorical_crossentropy',
              optimizer=Adam(),
              metrics=['accuracy'])
model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test))
# Test the model
score = model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
# Save the trained weights in to .h5 format
model.save_weights("Yolo_Basic_model2.h5")
print("Saved the model to disk")
model.save_weights("Yolo_Basic_model2.h5")
print("Saved the model to disk")
from google.colab import files

files.download('Yolo_Basic_model.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1716937354988399, 'fraction_numerical': 0.0962877030162413, 'mean_word_length': 6.99188876013905, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '403433', 'license': 'MIT', 'n_tokens_mistral': 3163, 'n_tokens_neox': 2783, 'n_words': 521, 'path': 'Week 3/Assignment 2/Assignment_3B.ipynb', 'repo_name': 'vgaurav3011/EIP-3.0-', 'size': '6896'}