import csv
import matplotlib.pyplot as plt
import numpy as np
from layers.dataset import cifar100
from layers.full import FullLayer
from layers.softmax import SoftMaxLayer
from layers.cross_entropy import CrossEntropyLayer
from layers.sequential import Sequential
from layers.relu import ReluLayer
from layers.conv import ConvLayer
from layers.flatten import FlattenLayer
from layers.maxpool import MaxPoolLayer

# getting training data and testing data
(x_train, y_train), (x_test, y_test) = cifar100(seed=1213351124)

# initialize the each layer for ML model
layer1 = ConvLayer(3, 16, 3)
relu1 = ReluLayer()
maxpool1= MaxPoolLayer()
layer2 = ConvLayer(16, 32, 3)
relu2 = ReluLayer()
maxpool2 = MaxPoolLayer()
loss1 = CrossEntropyLayer()
flatten = FlattenLayer()
layer3 = FullLayer(2048, 3)
softmax1 = SoftMaxLayer()
model = Sequential(
    (
        layer1,
Exemplo n.º 2
0
from layers.dataset import cifar100
from layers.full import FullLayer
from layers.softmax import SoftMaxLayer
from layers.cross_entropy import CrossEntropyLayer
from layers.sequential import Sequential
from layers.relu import ReluLayer
from layers.conv import ConvLayer
from layers.maxpool import MaxPoolLayer
from layers.flatten import FlattenLayer

import numpy as np
import matplotlib.pyplot as plt

(x_train, y_train), (x_test, y_test) = cifar100(1213076538)

layer1 = ConvLayer(3, 16, 3)
relu1 = ReluLayer()
layer2 = MaxPoolLayer(2)
layer3 = ConvLayer(16, 32, 3)
relu2 = ReluLayer()
layer4 = MaxPoolLayer(2)
layer5 = FlattenLayer()
layer6 = FullLayer(32*8*8, 4)
softmax = SoftMaxLayer()
loss = CrossEntropyLayer()


lr = 0.1
scores = []
n_epochs = 5
batch_size = 128
Exemplo n.º 3
0
import numpy as np
import matplotlib.pyplot as plt

from layers import (FullLayer, ReluLayer, SoftMaxLayer, CrossEntropyLayer)
from layers.sequential import Sequential
from layers.dataset import cifar100
(x_train, y_train), (x_test, y_test) = cifar100(1213268041)

test_accuracy=[]
epochs=20
lr=[0.1, 0.01, 0.2]
for i in lr:
    model = Sequential(layers=(FullLayer(32 * 32 * 3, 2500),
                               ReluLayer(),
                               FullLayer(2500, 2000),
                               ReluLayer(),
                               FullLayer(2000, 1500),
                               ReluLayer(),
                               FullLayer(1500, 1000),
                               ReluLayer(),
                               FullLayer(1000, 500),
                               ReluLayer(),
                               FullLayer(500, 4),
                               SoftMaxLayer()),
                       loss=CrossEntropyLayer())
    finalloss = model.fit(x_train, y_train,epochs=20, lr=i)
    y_pred = model.predict(x_test)
    accuracy=(np.mean(y_test == y_pred))
    print('Accuracy: %.2f' % accuracy)
    np.append(test_accuracy, accuracy)
    plt.plot(range(epochs), finalloss, label=('Learning rate=',i))
Exemplo n.º 4
0
 def load_data(self):
     # load data from cifar100 folder
     (x_train, y_train), (x_test, y_test) = cifar100(1211506319)
     return x_train, y_train, x_test, y_test
Exemplo n.º 5
0
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 25 19:52:43 2018

@author: kaushik
"""
import time
import numpy as np
import matplotlib.pyplot as plt
from layers.dataset import cifar100
from layers import (ConvLayer, FullLayer, FlattenLayer, MaxPoolLayer,
                    ReluLayer, SoftMaxLayer, CrossEntropyLayer, Sequential)

(x_train, y_train), (x_test, y_test) = cifar100(1337)
model = Sequential(layers=(ConvLayer(3, 16, 3), ReluLayer(), MaxPoolLayer(),
                           ConvLayer(16, 32, 3), ReluLayer(), MaxPoolLayer(),
                           FlattenLayer(), FullLayer(8 * 8 * 32,
                                                     4), SoftMaxLayer()),
                   loss=CrossEntropyLayer())
start_time = time.clock()
lr_vals = [0.1]
losses_train = list()
losses_test = list()
test_acc = np.zeros(len(lr_vals))
for j in range(len(lr_vals)):
    train_loss, test_loss = model.fit(x_train,
                                      y_train,
                                      x_test,
                                      y_test,
                                      epochs=8,
Exemplo n.º 6
0
import numpy as np
from layers.dataset import cifar100

# Please make sure that cifar-100-python is present in the same folder as dataset.py

(x_train, y_train), (x_test, y_test) = cifar100(1212356299)

from layers import (FullLayer, ReluLayer, SoftMaxLayer, CrossEntropyLayer,
                    Sequential_better)

model = Sequential_better(layers=(FullLayer(3072, 1500), ReluLayer(),
                                  FullLayer(1500, 500), ReluLayer(),
                                  FullLayer(500, 4), SoftMaxLayer()),
                          loss=CrossEntropyLayer())

loss2 = model.fit(x_train, y_train, lr=0.1, epochs=15)
y_predict = model.predict(x_test)

count = 0
for i in range(np.size(y_test)):
    if y_predict[i] == y_test[i]:
        count += 1

accuracy = (100.0 * count) / np.shape(y_predict)[0]

print "Accuracy of better CIFAR = ", accuracy, "%"
Exemplo n.º 7
0
from keras.models import Sequential
from keras.layers import Dense, Flatten, Activation
from keras.layers import Conv2D, MaxPooling2D
import numpy as np
import matplotlib.pyplot as plt
from layers.dataset import cifar100

batch_size = 128
num_classes = 4
epochs = 2000

"""load data"""
(x_train, y_train), (x_test, y_test) = cifar100(1211506319)
# flip dimension of x
x_train = np.transpose(x_train, (0, 2, 3, 1))
x_test = np.transpose(x_test, (0, 2, 3, 1))

input_shape = (32, 32, 3)

"""build cnn model"""
model = Sequential()

model.add(Conv2D(16, kernel_size=(3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
#model.add(Dense(4, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
Exemplo n.º 8
0
from __future__ import print_function
from layers.full import FullLayer
from layers.softmax import SoftMaxLayer
from layers.cross_entropy import CrossEntropyLayer
from layers.sequential import Sequential
from layers.relu import ReluLayer
from layers.dataset import cifar100
from sklearn.metrics import accuracy_score
import numpy as np

(x_train, y_train), (x_test, y_test) = cifar100(1213391684)

layer1 = FullLayer(3072, 2052)
relu1 = ReluLayer()
layer2 = FullLayer(2052, 680)
relu2 = ReluLayer()
layer3 = FullLayer(680, 4)
softmax = SoftMaxLayer()
loss = CrossEntropyLayer()
model = Sequential((layer1, relu1, layer2, relu2, layer3, softmax), loss)

model.fit(x_train, y_train, epochs=25, lr=0.06)
out = model.predict(x_test)
y = np.reshape(y_test, (x_test.shape[0], 1))
print("y_test.shape:", y.shape)
print("out.shape:", out.shape)
t = accuracy_score(y, out)
print(y_test, "Predicted Val", out)
print('accuracy score', t)
score = (np.mean(out == y))
print("score", score)