f.write(str(['Test loss: ', score[0]])) f.write('\n') f.write(str(['Test accuracy: ', score[1]])) f.write('\n') confusion = [] precision = [] recall = [] f1s = [] kappa = [] auc = [] roc = [] scores = np.array([np.argmax(t) for t in np.asarray(model.predict(X_testNP))]) predict = np.array([np.argmax(t) for t in np.round(np.asarray(model.predict(X_testNP)))]) targ = np.array([np.argmax(t) for t in Y_test]) confusion.append(sklm.confusion_matrix(targ.flatten(), predict.flatten())) precision.append(sklm.precision_score(targ.flatten(), predict.flatten(), average = 'macro')) recall.append(sklm.recall_score(targ.flatten(), predict.flatten(), average = 'macro')) f1s.append(sklm.f1_score(targ.flatten(), predict.flatten(), average = 'macro')) kappa.append(sklm.cohen_kappa_score(targ.flatten(), predict.flatten())) confusion.append(sklm.confusion_matrix(targ.flatten(), predict.flatten())) f.write(str(['Area Under ROC Curve (AUC): ', auc])) f.write('\n') f.write('Confusion: ') f.write('\n')
if K.image_dim_ordering() == 'th': # Transpose image dimensions (Theano uses the channels as the 1st dimension) im = im.transpose((2, 0, 1)) # Use pre-trained weights for Theano backend weights_path = '../imagenet_models/densenet169_weights_th.h5' else: # Use pre-trained weights for Tensorflow backend weights_path = '../imagenet_models/densenet169_weights_tf.h5' # Insert a new dimension for the batch_size im = np.expand_dims(im, axis=0) # Test pretrained model model = DenseNet(reduction=0.5, classes=1000, weights_path=weights_path) sgd = SGD(lr=1e-2, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy']) out = model.predict(im) # Load ImageNet classes file classes = [] with open('resources/classes.txt', 'r') as list_: for line in list_: classes.append(line.rstrip('\n')) print('Prediction: ' + str(classes[np.argmax(out)]))
weights_path = 'imagenet_models/densenet169_weights_th.h5' else: # Use pre-trained weights for Tensorflow backend weights_path = 'imagenet_models/densenet169_weights_tf.h5' # Insert a new dimension for the batch_size im = np.expand_dims(im, axis=0) # Test pretrained model model = DenseNet(reduction=0.5, classes=10, weights_path=weights_path) # Learning rate is changed to 1e-3 sgd = SGD(lr=1e-2, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy']) out = model.predict(im) # Load ImageNet classes file classes = [] with open('resources/classes.txt', 'r') as list_: for line in list_: classes.append(line.rstrip('\n')) print 'Prediction: '+str(classes[np.argmax(out)]) from keras.datasets import cifar10 (X_train, Y_train), (X_test, Y_test) = cifar10.load_data() img_size = 32 img_chan = 3 n_classes = 10
f.write(str(['Test loss: ', score[0]])) f.write('\n') f.write(str(['Test accuracy: ', score[1]])) f.write('\n') confusion = [] precision = [] recall = [] f1s = [] kappa = [] auc = [] roc = [] scores = np.asarray(model.predict(X_test)) predict = np.round(np.asarray(model.predict(X_test))) targ = Y_test auc.append(sklm.roc_auc_score(targ.flatten(), scores.flatten())) confusion.append(sklm.confusion_matrix(targ.flatten(), predict.flatten())) precision.append(sklm.precision_score(targ.flatten(), predict.flatten())) recall.append(sklm.recall_score(targ.flatten(), predict.flatten())) f1s.append(sklm.f1_score(targ.flatten(), predict.flatten())) kappa.append(sklm.cohen_kappa_score(targ.flatten(), predict.flatten())) f.write(str(['Area Under ROC Curve (AUC): ', auc])) f.write('\n') f.write('Confusion: ') f.write('\n') f.write(str(np.array(confusion)))
f.write(str(['Test loss: ', score[0]])) f.write('\n') f.write(str(['Test accuracy: ', score[1]])) f.write('\n') confusion = [] precision = [] recall = [] f1s = [] kappa = [] auc = [] roc = [] scores = np.array( [np.argmax(t) for t in np.asarray(model.predict(X_test))]) predict = np.array( [np.argmax(t) for t in np.round(np.asarray(model.predict(X_test)))]) targ = np.array([np.argmax(t) for t in Y_test]) auc.append(sklm.roc_auc_score(targ.flatten(), scores.flatten())) confusion.append(sklm.confusion_matrix(targ.flatten(), predict.flatten())) precision.append(sklm.precision_score(targ.flatten(), predict.flatten())) recall.append(sklm.recall_score(targ.flatten(), predict.flatten())) f1s.append(sklm.f1_score(targ.flatten(), predict.flatten())) kappa.append(sklm.cohen_kappa_score(targ.flatten(), predict.flatten())) confusion.append(sklm.confusion_matrix(targ.flatten(), predict.flatten())) f.write(str(['Area Under ROC Curve (AUC): ', auc])) f.write('\n')