**This is my code below i am not sure what i am doing wrong .I need some help with this **
this is the code i am implementing on
"https://github.com/prog-eval/prog-eval/blob/master/convnet/main-redacted.py "
I am getting an error on return model and on this line X_train = np.load(‘data/cifar2/X_train.npy’)
Convnet/main.py
import sys
import json
import argparse
import numpy as np
from time import time
# --
# User code
# Note: Depending on how you implement your model, you'll likely have to change the parameters of these
# functions. They way their shown is just one possble way that the code could be structured.
# ... your code here ...
def catdog():
model = Sequential()
model.add(Convolution2D(32, 3, 3, border_mode='same', input_shape=(3, ROWS, COLS), activation='relu'))
model.add(Convolution2D(32, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(Convolution2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(128, 3, 3, border_mode='same', activation='relu'))
model.add(Convolution2D(128, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))
# model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))
# model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))
# model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))
# model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss=objective, optimizer=optimizer, metrics=['accuracy'])
return model
model = catdog()
def make_train_dataloader(X, y, batch_size, shuffle):
# ... your code here ...
nb_epoch = 10
batch_size = 16
## Callback for loss logging per epoch
class LossHistory(Callback):
def on_train_begin(self, logs={}):
self.losses = []
self.val_losses = []
def on_epoch_end(self, batch, logs={}):
self.losses.append(logs.get('loss'))
self.val_losses.append(logs.get('val_loss'))
early_stopping = EarlyStopping(monitor='val_loss', patience=3, verbose=1, mode='auto')
return dataloader
def make_test_dataloader(X, batch_size, shuffle):
# ... your code here ...
def run_catdog():
history = LossHistory()
model.fit(train, labels, batch_size=batch_size, nb_epoch=nb_epoch,
validation_split=0.25, verbose=0, shuffle=True, callbacks=[history, early_stopping])
predictions = model.predict(test, verbose=0)
return predictions, history
predictions, history = run_catdog()
return dataloader
def train_one_epoch(model, dataloader):
# ... your code here ...
loss = history.losses
val_loss = history.val_losses
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('VGG-16 Loss Trend')
plt.plot(loss, 'blue', label='Training Loss')
plt.plot(val_loss, 'green', label='Validation Loss')
plt.xticks(range(0,nb_epoch)[0::2])
plt.legend()
plt.show()
return model
def predict(model, dataloader):
# ... your code here ...
for i in range(0,10):
if predictions[i, 0] >= 0.5:
print('I am {:.2%} sure this is a Dog'.format(predictions[i][0]))
else:
print('I am {:.2%} sure this is a Cat'.format(1-predictions[i][0]))
plt.imshow(test[i].T)
plt.show()
return predictions
# --
# CLI
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--cuda', action="store_true")
parser.add_argument('--num-epochs', type=int, default=5)
parser.add_argument('--lr', type=float, default=0.1)
parser.add_argument('--momentum', type=float, default=0.9)
parser.add_argument('--batch-size', type=int, default=128)
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
# --
# IO
# X_train: tensor of shape (number of train observations, number of image channels, image height, image width)
# X_test: tensor of shape (number of train observations, number of image channels, image height, image width)
# y_train: vector of [0, 1] class labels for each train image
# y_test: vector of [0, 1] class labels for each test image (don't look at these to make predictions!)
X_train = np.load('data/cifar2/X_train.npy')
X_test = np.load('data/cifar2/X_test.npy')
y_train = np.load('data/cifar2/y_train.npy')
y_test = np.load('data/cifar2/y_test.npy')
# --
# Define model
model = catdog(
optimizer = RMSprop(lr=1e-4)
objective = 'binary_crossentropy'
)
# --
# Train
t = time()
for epoch in range(args.num_epochs):
# Train
model = train_one_epoch(
model=model,
dataloader=make_train_dataloader(X_train, y_train, batch_size=args.batch_size, shuffle=True)
)
# Evaluate
preds = predict(
model=model,
dataloader=make_test_dataloader(X_test, batch_size=args.batch_size, shuffle=False)
)
assert isinstance(preds, np.ndarray)
assert preds.shape[0] == X_test.shape[0]
test_acc = (preds == y_test.squeeze()).mean()
print(json.dumps({
"epoch" : int(epoch),
"test_acc" : test_acc,
"time" : time() - t
}))
sys.stdout.flush()
elapsed = time() - t
print('elapsed', elapsed, file=sys.stderr)
# --
# Save results
os.makedirs('results', exist_ok=True)
np.savetxt('results/preds', preds, fmt='%d')
open('results/elapsed', 'w').write(str(elapsed))