# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/Abhishek1236/computer-vision-models/blob/main/Alexnet_practice.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="xowVGIa68DSw" outputId="dceeb2f0-0022-4cbd-c38f-e859dd425fea" colab={"base_uri": "https://localhost:8080/"}
#importing the libraries
import keras
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, Flatten,\
 Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
import numpy as np
import tflearn.datasets.oxflower17 as oxflower17

import tensorflow as tf
np.random.seed(1000)

# + id="nXijRQhVoF-N"
# !pip install -r requirements.txt

# + id="f1zWKuOQpE1A" outputId="962052b7-4723-4ee8-d558-0f0d7e68d841" colab={"base_uri": "https://localhost:8080/"}
# !pip install tflearn

# + colab={"base_uri": "https://localhost:8080/"} id="K0p1K8dV8Mi_" outputId="959351f4-fba7-43ca-c7bf-d67d7a7ca5a6"
#preparing the data 
x, y = oxflower17.load_data(one_hot=True)


# + id="AlTY6R0O8lqU"
#Model 
class Alexnet:
  def __init__(self,batch_size,epoch,validation_split,x,y):
    self.batch_size = batch_size
    self.epoch = epoch
    self.validation_split = validation_split
    self.x = x
    self.y = y
  def forward():
    model = Sequential()
    model.add(Conv2D(96,input_shape=(224,224,3),kernel_size=(11,11),strides=(4,4),padding='valid'))# first conv
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(3,3),strides=(2,2),padding='valid'))
    model.add(BatchNormalization())
    model.add(Conv2D(256,kernel_size=(5,5),padding='same'))# second conv
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(3,3),strides=(1,1)))
    model.add(BatchNormalization())
    model.add(Conv2D(384,kernel_size=(3,3),padding="same",strides=(1,1)))#third conv
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(3,3),padding='valid'))
    model.add(BatchNormalization())
    model.add(Conv2D(256,kernel_size=(3,3),padding='valid'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(3,3),strides=(2,2),padding='valid'))
    model.add(BatchNormalization())
    model.add(Flatten())
    model.add(Dense(4096))
    model.add(Activation('relu'))
    model.add(Dense(4096))
    model.add(Activation('relu'))
    model.add(Dense(17))
    model.add(Activation('softmax'))
    return model
  def training(x,y,batch_size,epochs,validation_split,model):
    model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])
    model.fit(x,y, batch_size=batch_size, epochs=epochs, verbose=1,validation_split=validation_split, shuffle=True)
    return model



# + colab={"base_uri": "https://localhost:8080/"} id="2fzBLAzQCO07" outputId="af020c0a-9f10-48bf-eb6b-238a62570711"

#printing the summary of the model
model = Alexnet.forward()
model.summary()

# + colab={"base_uri": "https://localhost:8080/"} id="b4O_tIVaCX9f" outputId="f927be63-345b-4578-f118-d97156e69574"
trained_model = Alexnet.training(x = x,y = y,batch_size=64,epochs= 25,validation_split=0.25,model=model)

# + id="x3oPfsTvG7lN"
#saving to disk
trained_model.save('Alexnet.h5')

# + id="wSkmTpYWHoIr"


StarcoderdataJupyterScriptsDedupFiltered