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

import numpy as np
from keras.datasets import cifar10
from sklearn.preprocessing import OneHotEncoder
from tqdm import tqdm_notebook as tqdm
from keras.layers import Layer, Input, Conv2D, MaxPool2D, Flatten, Dense, Reshape
from keras.models import Model
from src.gaussian_attention_layer import VisualAttentionLayer

# ### Load data

# +
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
oh_enc = OneHotEncoder(n_values=10, sparse=False)
y_train = oh_enc.fit_transform(y_train)
y_test = oh_enc.transform(y_test)

x_train = x_train/128.0-1.0
x_test = x_test/128.0-1.0
# -

# ### Create Model

# +
inputs = Input(shape=(32,32,3))

x = Conv2D(64, kernel_size=(3,3), activation="relu")(inputs)
x = MaxPool2D()(x)
x = Conv2D(64, kernel_size=(3,3), activation="relu")(x)
x = MaxPool2D()(x)
x = Flatten()(x)
x = Dense(6, activation="relu")(x)
x = VisualAttentionLayer(output_dim=(20,20))([inputs, x])

x = Conv2D(64, kernel_size=(3,3), activation="relu")(x)
x = MaxPool2D()(x)
x = Conv2D(64, kernel_size=(3,3), activation="relu")(x)
x = Flatten()(x)
predictions = Dense(10, activation='softmax')(x)

model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.summary()
# -

# ### Train the Model

history = model.fit(x_train, y_train, epochs=5, batch_size=32, verbose=2, validation_data=(x_test, y_test))  



StarcoderdataJupyterScriptsDedupFiltered