def evaluation(args): path_img_val = '../datasets/ilsvrc2012/images/val/' path_val_info = '../datasets/ilsvrc2012/images/val.txt' if args.model == 'vgg16': model = VGG16(weights='imagenet') model.summary() elif args.model == 'resnet152': model = ResNet152(weights='imagenet') model.summary() elif args.model == 'resnet152v2': model = ResNet152V2(weights='imagenet') model.summary() elif args.model == 'inceptionresnetv2': model = InceptionResNetV2(weights='imagenet') model.summary() elif args.model == 'densenet201': model = DenseNet201(weights='imagenet') model.summary() elif args.model == 'nasnetlarge': model = NASNetLarge(weights='imagenet') model.summary() name, label = load_header_imagenet(load_file(path_val_info)) pred = list() for i, n in enumerate(name): x = preprocessing_imagenet(path_img_val + n, args) pred.append(np.argmax(model.predict(x), axis=1)[0]) if i % 1000 == 0: print(n) correct = len([p for p, l in zip(pred, label) if p == l]) print('Accuracy of the IMAGENET dataset using model %s: %.4f' % (args.model, correct / len(label)))
class Model: def __init__(self, debug=False): self.debug = debug if debug: return model_path = "{}/model/model.h5".format(os.getcwd()) if not os.path.isfile(model_path): print("no model file") exit(1) print("loading model") self.classifier = load_model(model_path) self.nas = NASNetLarge(input_shape=None, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000) self.nas.layers.pop() self.nas.outputs = [self.nas.layers[-1].output] self.nas.layers[-1].outbound_nodes = [] def predict(self, segment): if self.debug: return 1 features = self.nas.predict(np.array(segment)) result = self.classifier.predict( np.array(features).reshape(1, FRAME_INTERVAL, 4032)) return np.argmax(result[0], axis=0)
def predict(image): model = NASNetLarge() pred = model.predict(image) decoded_predictions = decode_predictions(pred, top=10) response = 'NASNetLarge predictions: ' + str(decoded_predictions[0][0:5]) print(response) np.argmax(pred[0]) return response
def nasenet(image): file_name = 'object' image = load_img(file_name, target_size=(331, 331)) image = np.expand_dims(image, axis=0) image = nas(image) model = NASNetLarge() predection = model.predict(image) preddected = imagenet_utils.decode_predictions(predection) preddected = preddected[0][0] preddected = { "model": 'nasenet', "object_detected": preddected[1], "accurecy": preddected[2] * 100 } return preddected
def extract_features(directory, ids, model): if int(model) == 1: print("1") # load ResNet50 model model = ResNet50() input_size = 224 else: print("2") # load NASNetLarge model model = NASNetLarge(input_shape=(331, 331, 3), include_top=True, weights='imagenet', input_tensor=None, pooling=None) input_size = 331 # pops the last layer to get the features model.layers.pop() model = Model(inputs=model.inputs, outputs=model.layers[-2].output) model.summary() print(len(model.layers)) # model characteristics plot_model(model, to_file='model.png') imgs = load_list(ids) print('Dataset: %d' % len(imgs)) N = len(imgs) print(N) results = [] i = 0 batch_size = 1 # this can be 8 for a GTX 1080 Ti and 32G of RAM while i < N: if i % 1024 == 0: print('{} from {} images.'.format(i, N)) batch = imgs[i:i + batch_size] i += batch_size images = [ load_img(os.path.join(directory, img + ".jpg"), target_size=(input_size, input_size)) for img in batch ] images = [preprocess_input(img_to_array(img)) for img in images] images = np.stack(images) r = model.predict(images) for ind in range(batch_size): results.append(r[ind]) return results
class Predictor: def __init__(self): self.model = NASNetLarge() def labels_to_percents(self, labels) -> List: """ Attach percents to labels """ return [[x[1], round(x[2] * 100, 2)] for x in labels[0]] def predict(self, uploaded_image: TemporaryUploadedFile) -> (List, Image): """ Read image and predict category """ # convert the image pixels to a numpy array image = img_to_array(Image.open(uploaded_image)) # resize (crop) to vgg16 size (331, 331, 3) image = smart_resize(image, (331, 331)) # grab a RGB preview for frontend rgb_preview = Image.fromarray(np.uint8(image)).convert('RGB') # reshape data for the model, add new axis (1, 224, 224, 3) image = np.expand_dims(image, axis=0) # prepare the image for the VGG model (subtracts the mean RGB channels) image = preprocess_input(image) # predict the probability across all output classes what = self.model.predict(image) # convert the probabilities to class labels labels = decode_predictions(what, top=3) # make it readable labels = self.labels_to_percents(labels) return labels, rgb_preview
alpha=0.95 pred=0 idscreen=0 while(True): # Capture frame-by-frame ret, frame = cap.read() # img=np.array(cv2.resize(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB),(224,224))) img=np.array(cv2.resize(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB),(331,331))) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) pred = alpha*pred+(1-alpha)*model.predict(x) txt=decode_predictions(pred,10) #print txt for i,p in enumerate(txt[0]): cv2.putText(frame,"{:.3f}: {}".format(float(p[2]),p[1]),(0,25*i+30),cv2.FONT_HERSHEY_PLAIN,2,[1,1,1]) # Display the resulting frame cv2.imshow('frame',frame) key=cv2.waitKey(1) if (key & 0xFF) in [ ord(' '),ord('q')]: break if (key & 0xFF) in [ ord('s')]: cv2.imwrite("screen_{}.png".format(idscreen),frame) idscreen+=1
callbacks=[checkpointer,lr_reduction,estopping]) history_plot(history) RNmodel.load_weights(fw) RNmodel.evaluate(pvx_test1,y_test1) from keras.applications.nasnet import NASNetLarge NNLbmodel=NASNetLarge(weights='imagenet',include_top=False) n1,n2=1800,180 def resh(x): y=[resize(el,(int(331),int(331),int(3)), anti_aliasing=True) for el in x] return np.array(y) pvx_train1,pvx_valid1,pvx_test1=\ resh(x_train1[:n1]),resh(x_valid1[:n2]),resh(x_test1[:n2]) pvx_train1=NNLbmodel.predict(pvx_train1) pvx_valid1=NNLbmodel.predict(pvx_valid1) pvx_test1=NNLbmodel.predict(pvx_test1) pvx_train1.shape sh=pvx_train1.shape[1:] def NNLmodel(): model=Sequential() model.add(GlobalAveragePooling2D(input_shape=sh)) model.add(Dense(2048)) model.add(LeakyReLU(alpha=.02)) model.add(Dropout(.25)) model.add(Dense(512)) model.add(LeakyReLU(alpha=.02)) model.add(Dropout(.25))
# Load our model # model = densenet169_model(img_rows=img_rows, img_cols=img_cols, color_type=channel, num_classes=num_classes) # load keras model model = NASNetLarge(weights=None, classes=10) sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy']) model.summary() # Start Fine-tuning model.fit( X_train, Y_train, batch_size=batch_size, epochs=nb_epoch, shuffle=True, verbose=1, validation_data=(X_valid, Y_valid), ) # Make predictions predictions_valid = model.predict(X_valid, batch_size=batch_size, verbose=1) # Cross-entropy loss score score = log_loss(Y_valid, predictions_valid)
from keras.models import load_model #model = load_model('/content/drive/My Drive/ColabNotebooks/AllmodeloRMSpropXception.h5') model = load_model('/content/drive/My Drive/ColabNotebooks/NasNet/modelNasNet.h5') # ## Predict the Model Trained # **Show the three better images and the three wrong image of the test set** # In[13]: predictTest = model.predict(x_test, verbose=1) predictTest = predictTest.reshape(predictTest.shape[0]) #predictTest = predictTest.astype('int32') #print(x_test.shape) #print(predictTest.shape) #print(y_test.shape) #print(np.round(predictTest)) #print(y_test) #print(predictTest) mae = np.abs(y_test - predictTest) #print(mae) pos = np.argsort(mae) print(pos[-1]) print(pos[-2])
from keras.applications.nasnet import preprocess_input from keras.applications.nasnet import decode_predictions from keras.applications.nasnet import NASNetLarge model = NASNetLarge(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) for i in range(0, 5): print('-----------------------------------------------') # load an image from file image = load_img('../resources/wolf_' + str(i + 1) + '.jpg', target_size=(331, 331)) # convert the image pixels to a numpy array image = img_to_array(image) # reshape data for the model image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) # prepare the image for the VGG model image = preprocess_input(image) # predict the probability across all output classes yhat = model.predict(image) # convert the probabilities to class labels label = decode_predictions(yhat) for i in range(0, len(label[0])): # retrieve the most likely result, e.g. highest probability labelTemp = label[0][i] # print the classification print('%s (%.2f%%)\n' % (labelTemp[1], labelTemp[2] * 100))
from keras.applications.nasnet import preprocess_input, decode_predictions, NASNetLarge from keras.preprocessing import image import numpy as np model = NASNetLarge(weights='imagenet') img_path = '../../../Data/VGGFacesV2/train/n000002/0021_01.jpg' img = image.load_img(img_path, target_size=(331, 331)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) # decode the results into a list of tuples (class, description, probability) # (one such list for each sample in the batch) print('Predicted:', decode_predictions(preds, top=3)[0])
#### NASnetLarge """ # creating bottleneck features x_train=np.array([misc.imresize(x_train[i],(331,331,3)) for i in range(0,len(x_train))]).astype('float32') x_valid=np.array([misc.imresize(x_valid[i],(331,331,3)) for i in range(0,len(x_valid))]).astype('float32') x_test=np.array([misc.imresize(x_test[i],(331,331,3)) for i in range(0,len(x_test))]).astype('float32') x_train=nnpi(x_train) x_valid=nnpi(x_valid) x_test=nnpi(x_test) fn="../input/keras-applications-weights/nasnet_large_no_top.h5" nasnet_base_model=NASNetLarge(weights=fn,include_top=False) x_train=nasnet_base_model.predict(x_train) x_valid=nasnet_base_model.predict(x_valid) x_test=nasnet_base_model.predict(x_test) def nasnet_model(): model = Sequential() model.add(GlobalAveragePooling2D(input_shape=x_train.shape[1:])) model.add(Dense(2048)) model.add(LeakyReLU(alpha=.02)) model.add(Dropout(.5)) model.add(Dense(256)) model.add(LeakyReLU(alpha=.02)) model.add(Dropout(.5)) model.add(Dense(33,activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='nadam',metrics=['accuracy'])
import os import numpy as np from keras.applications.nasnet import NASNetLarge from imageio import imread from scipy.misc import imresize file_names = os.listdir('dataset/') image_shape = (331, 331, 3) model = NASNetLarge(input_shape=image_shape, include_top=False, pooling='avg') batch_size = 16 for i in range(0, len(file_names), batch_size): batch = file_names[i:i + batch_size] x_batch = np.zeros(((len(batch), ) + image_shape), dtype='float') for j, fn in enumerate(batch): print(fn) img = imread('dataset/' + fn, pilmode="RGB") img = imresize(img, image_shape) x_batch[j] = img x_batch = x_batch / 127.5 - 1 prediction = model.predict(x_batch) for j, fn in enumerate(batch): np.save('vectors/' + fn + '.npy', prediction[j])