def generate_training(): global sampler while True: X, Y = sampler.train(add_noise=True) X_a = preprocess_input(X[:, :, :, 0:3]) X_b = preprocess_input(X[:, :, :, 3:6]) yield ([X_a, X_b], Y[:, 0])
def main(): labels = pd.read_csv(data_dir + 'labels.csv') num_classes = len(labels.groupby('breed')) selected_labels = labels.groupby('breed').count().sort_values( by='id', ascending=False).head(num_classes).index.values labels = labels[labels['breed'].isin(selected_labels)] labels['target'] = 1 labels['rank'] = labels.groupby('breed').rank()['id'] labels_pivot = labels.pivot('id', 'breed', 'target').reset_index().fillna( 0) # values必须是breed和target对应的值 np.random.seed(SEED) y_train = labels_pivot[selected_labels].values ytr = y_train x_train = np.zeros((len(labels), INPUT_SIZE, INPUT_SIZE, 3), dtype='float32') for i, img_id in tqdm(enumerate(labels['id'])): # print i, img_id img = read_img(img_id, 'train', (INPUT_SIZE, INPUT_SIZE)) x = xception.preprocess_input(np.expand_dims(img.copy(), axis=0)) x_train[i] = x print('Train Images shape: {} size: {:,}'.format(x_train.shape, x_train.size)) num_tests = len(listdir(data_dir + '/test/')) x_test = np.zeros((num_tests, INPUT_SIZE, INPUT_SIZE, 3), dtype='float32') test_id = [] for i in range(num_tests): img_file_name = listdir(data_dir + '/test/')[i] img_id = img_file_name[0:len(img_file_name) - 4] img = read_img(img_id, 'test', (INPUT_SIZE, INPUT_SIZE)) x = xception.preprocess_input(np.expand_dims(img.copy(), axis=0)) x_test[i] = x test_id.append(img_id) xtr = x_train xception_bottleneck = xception.Xception(weights='imagenet', include_top=False, pooling=POOLING) train_x_bf = xception_bottleneck.predict(xtr, batch_size=32, verbose=1) valid_x_bf = xception_bottleneck.predict(x_test, batch_size=32, verbose=1) inception_bottleneck = inception_v3.InceptionV3(weights='imagenet', include_top=False, pooling=POOLING) train_i_bf = inception_bottleneck.predict(xtr, batch_size=32, verbose=1) valid_i_bf = inception_bottleneck.predict(x_test, batch_size=32, verbose=1) train_x = np.hstack([train_x_bf, train_i_bf]) test_x = np.hstack([valid_x_bf, valid_i_bf]) data = { 'train_x': train_x, 'train_y': ytr, 'test_x': test_x, "num_class": num_classes, "selected_labels": selected_labels, "test_id": test_id } with open('xicpt_data.pickle', 'wb') as handle: pickle.dump(data, handle, protocol=pickle.HIGHEST_PROTOCOL)
def generate_test(): global sampler while True: X, Y = sampler.test() X_a = preprocess_input(X[:, :, :, 0:3]) X_b = preprocess_input(X[:, :, :, 3:6]) yield ([X_a, X_b], Y[:, 0])
def get_training_data_xception(): filenames = os.listdir(TRAIN_PATH) shuffle(filenames) X_train = [] y_train = [] total_hate = 0 total_no_hate = 0 print '*****TRAIN*****' for filename in filenames: if "no_hate" in filename: y_train.append([1, 0]) total_no_hate += 1 else: y_train.append([0, 1]) total_hate += 1 img = image.load_img(TRAIN_PATH + filename, target_size=(299, 299)) x = image.img_to_array(img) x = xception.preprocess_input(x) X_train.append(x) print 'Total train examples: ' + str(len(y_train)) print 'No hate examples: ' + str(total_no_hate) print 'Hate examples: ' + str(total_hate) print '*****VAL*****' filenames = os.listdir(VAL_PATH) shuffle(filenames) X_val = [] y_val = [] total_hate = 0 total_no_hate = 0 for filename in filenames: if "no_hate" in filename: y_val.append([1, 0]) total_no_hate += 1 else: y_val.append([0, 1]) total_hate += 1 img = image.load_img(VAL_PATH + filename, target_size=(299, 299)) x = image.img_to_array(img) x = xception.preprocess_input(x) X_val.append(x) print 'Total val examples: ' + str(len(y_val)) print 'No hate examples: ' + str(total_no_hate) print 'Hate examples: ' + str(total_hate) X_train = np.array(X_train) y_train = np.array(y_train) X_val = np.array(X_val) y_val = np.array(y_val) return X_train, y_train, X_val, y_val
def prepare_image(img): img = img_to_array(img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) # return the processed image return img
def detect_dog_img(self, img): """Detect whether or not image contains dog""" # img needs to be resized to 299, base xception input shape img = preprocess_input(Util.img_to_tensor(img, img_resize=299)) base_model = Xception(weights='imagenet') prediction = np.argmax(base_model.predict(img)) return bool(prediction >= 151 and prediction <= 268)
def extract_features(directory): dir1=directory #extracting features model=Xception() #removing the last output layer model.layers.pop() #defining the Model which will take the input the size of the imahe and output the values in the last layer model=Model(inputs=model.inputs,output=model.layers[-1].output) print(model.summary()) features=dict() file=os.listdir(dir1) for name in file: path=os.path.join(dir1,name) image=load_img(path=path,target_size=(299,299)) #conv to np array image=img_to_array(image) image=image.reshape((1,image.shape[0],image.shape[1],image.shape[2])) #preprocessing the input image according to the Xception model image=preprocess_input(image) feature=model.predict(image) #get the image id image_id=name.split('.')[0] features[image_id]=feature print('>%s'%name) return features
def data_generator(data, batch_size): # 样本生成器,节省内存 while True: # 生成一个从x中抽取的随机数,维度为y的向量,y为抽取次数 batch = np.random.choice(data, batch_size) x, y = [], [] for img in batch: x.append(misc.imresize(misc.imread(img), img_size)) # 读取resize图片,再存进x列表 imglabel = str(img).split('\\')[1].split('.')[0] label = str(imglabel) # print(label) y_list = [] for i in label: # i = i.upper() if 48 <= ord(i) <= 57: y_list.append(ord(i) - 48) if 65 <= ord(i) <= 90: y_list.append(ord(i) - 55) if 97 <= ord(i) <= 122: y_list.append(ord(i) - 61) y.append(y_list) # 把验证码标签添加到y列表,把对应字母转化为数字 x = preprocess_input(np.array(x).astype(float)) # 原先是dtype=uint8转成一个纯数字的array y = np.array(y) yield x, [y[:, i] for i in range(4)]
def extract_vgg16_features(x): from keras.applications.vgg16 import preprocess_input, VGG16 print(x.shape) # im_h = x.shape[1] im_h = 224 model = VGG16(include_top=True, weights='imagenet', input_shape=(im_h, im_h, 3)) # if flatten: # add_layer = Flatten() # else: # add_layer = GlobalMaxPool2D() # feature_model = Model(model.input, add_layer(model.output)) feature_model = Model(model.input, model.get_layer('fc2').output) print('extracting features...') x = np.asarray([ img_to_array(array_to_img(im, scale=False).resize((im_h, im_h))) for im in x ]) x = preprocess_input(x) # data - 127. #data/255.# features = feature_model.predict(x) print('Features shape = ', features.shape) return features
def uploaded_file(filename): #import labels NUM_CLASSES = 120 labels = pd.read_csv('./FlaskWeb/labels.csv') selected_breed_list = list( labels.groupby('breed').count().sort_values( by='id', ascending=False).head(NUM_CLASSES).index) #preprocess the image img_path = join(app.config['UPLOAD_FOLDER'], filename) img = image.load_img(img_path, target_size=(299, 299)) img = image.img_to_array(img) img_prep = xception.preprocess_input(np.expand_dims(img.copy(), axis=0)) #make predictions imgX = xception_bottleneck.predict(img_prep, batch_size=32, verbose=1) img_stack = np.hstack([imgX, imgX]) #originally used an ensemble of inception & xception here but can only load one on Azure prediction = logreg.predict(img_stack) #plot image and prediction fig, ax = plt.subplots(figsize=(5, 5)) ax.imshow(img / 255.) breed = selected_breed_list[int(prediction)] ax.text(10, 250, 'Prediction: %s' % breed, color='k', backgroundcolor='g', alpha=0.8) ax.axis('off') output = io.BytesIO() fig.savefig(output) output.seek(0) return send_file(output, mimetype='image/png')
def main(model, rawImage, rgbImage, graph, state): img_width = 224 img_height = 224 # img_tmp = img rgbImage = cv2.resize(rgbImage, dsize=(img_width, img_height)) rgbImage = np.expand_dims(rgbImage, axis=0) rgbImage = preprocess_input(rgbImage) # 정규화 # predicted classes → 0 - Fire, 1 - Normal_state, 2 - Smoke, 3 - Fire-Smoke # 원본 이미지 predict # preds_org = model.predict(rgbImage) # numpy array, shape(1, 3) - [[a, b, c]] # predicted_class_org = preds_org.argmax(axis=1)[0] # 행별 최대값의 인덱스 if state == 'Fire': predicted_class_org = 0 # elif state == 'Smoke': # predicted_class_org = 2 conv_name = 'conv2d_4' # 마지막 Convolution layer grad_cam_org, grad_val_org = generate_gradcam(rgbImage, model, predicted_class_org, conv_name, graph) # 이미지 출력 rawImage = cv2.resize(rawImage, (img_width, img_height)) # 3차원 grad_cam_org = cv2.resize(grad_cam_org, (img_width, img_height)) # 2차원 grad_cam_org = (grad_cam_org - grad_cam_org.min()) / (grad_cam_org.max() - grad_cam_org.min()) grad_cam_org = grad_cam_org * 255 # 역정규화 grad_cam_org = grad_cam_org.astype(np.uint8) # 값을 정수로 변경 grad_cam_org = cv2.applyColorMap(grad_cam_org, cv2.COLORMAP_JET) # RGB 채널로 변경 grad_cam_org = cv2.addWeighted(rawImage, 0.5, grad_cam_org, 0.5, 0.0) return grad_cam_org
def uploaded_file(filename): #import labels NUM_CLASSES = 120 labels = pd.read_csv('./labels.csv') selected_breed_list = list(labels.groupby('breed').count().sort_values(by='id', ascending=False).head(NUM_CLASSES).index) # to make new predictions, run code below this line img_path = join(app.config['UPLOAD_FOLDER'], filename) img = image.load_img(img_path, target_size=(299, 299)) img = image.img_to_array(img) img_prep = xception.preprocess_input(np.expand_dims(img.copy(), axis=0)) imgX = xception_bottleneck.predict(img_prep, batch_size=32, verbose=1) imgI = inception_bottleneck.predict(img_prep, batch_size=32, verbose=1) img_stack = np.hstack([imgX, imgI]) prediction = logreg.predict(img_stack) #plot image and prediction fig, ax = plt.subplots(figsize=(5,5)) ax.imshow(img / 255.) breed = selected_breed_list[int(prediction)] ax.text(10, 250, 'Prediction: %s' % breed, color='k', backgroundcolor='g', alpha=0.8) ax.axis('off') output = io.BytesIO() fig.savefig(output) output.seek(0) if os.path.exists(img_path): os.remove(img_path) #delete user input return send_file(output, mimetype='image/png') #return the output image to be called in html
def load_image(path, target_size=(299, 299)): x = image.load_img(path, target_size=target_size) x = image.img_to_array(x) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # 정규화 return x
def xception_reg(): if request.method == 'POST': if 'file' not in request.files: return 'Error: No file part, please try again' file = request.files['file'] if file.filename == '': return 'Error: No file name, please try again' if file and allowed_file(file.filename): K.clear_session() model = load_model('models/transfer_CNN_reg.h5', custom_objects={ 'root_mean_squared_error': root_mean_squared_error }) img = image.load_img(file, target_size=(800, 800)) x = image.img_to_array(img) x = preprocess_input(x) x = expand_dims(x, axis=0) pred = model.predict(x)[0][0] ft = int(pred // 12) inch = round(pred % 12, 1) # process image for html img = Image.open(file) tag = create_tag(img) return render_template('prediction_answer.html', variable=[f'{ft} ft {inch} in', '', tag]) else: return redirect(request.url) return render_template('prediction.html', Title='Regression Prediction with Xception')
def extract_Xception(file_paths): """Manually extract pre-trained Xception bottleneck features""" tensors = Util.paths_to_tensors(file_paths).astype('float32') preprocessed_input = preprocess_input(tensors) return Xception(weights='imagenet', include_top=False).predict(preprocessed_input, batch_size=32)
def test_pipeline(self): """ Pipeline should provide correct function composition """ img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg')) xcpt_model = Xception(weights="imagenet") stages = [('spimage', gfac.buildSpImageConverter(SparkMode.RGB_FLOAT32)), ('xception', GraphFunction.fromKeras(xcpt_model))] piped_model = GraphFunction.fromList(stages) for fpath in img_fpaths: target_size = tuple(xcpt_model.input.shape.as_list()[1:-1]) img = load_img(fpath, target_size=target_size) img_arr = np.expand_dims(img_to_array(img), axis=0) img_input = xcpt.preprocess_input(img_arr) preds_ref = xcpt_model.predict(img_input) spimg_input_dict = imageArrayToStruct(img_input).asDict() spimg_input_dict['data'] = bytes(spimg_input_dict['data']) with IsolatedSession() as issn: # Need blank import scope name so that spimg fields match the input names feeds, fetches = issn.importGraphFunction(piped_model, prefix="") feed_dict = dict( (tnsr, spimg_input_dict[tfx.op_name(tnsr, issn.graph)]) for tnsr in feeds) preds_tgt = issn.run(fetches[0], feed_dict=feed_dict) # Uncomment the line below to see the graph # tfx.write_visualization_html(issn.graph, # NamedTemporaryFile(prefix="gdef", suffix=".html").name) self.assertTrue(np.all(preds_tgt == preds_ref))
def model_predict(img_path, model): img = image.load_img(img_path, target_size=(299, 299)) x = image.img_to_array(img) # Preprocessing the image x = np.expand_dims(x, axis=0) # x = np.true_divide(x, 255) preds = model.predict(preprocess_input(x)) return preds
def getCaption(fileName): print(fileName) WIDTH = 299 HEIGHT = 299 image = load_img(fileName, target_size=(WIDTH,HEIGHT)) image = img_to_array(image) image.shape = (1,) + image.shape image = preprocess_input(image) imageInput = imageModel.predict(image)[0] maxLen = 34 sentence = nextWord = 'startsentence' intWord = w2i[nextWord] textInput = pad_sequences([[intWord]], maxlen=maxLen)[0] for _ in range(maxLen): prediction = mainModel.predict([[imageInput], [textInput]]) nextWord = i2w[np.argmax(prediction)] sentence += ' ' + nextWord if nextWord == 'endsentence': break textInput = list(textInput) textInput.append(np.argmax(prediction)) textInput.pop(0) textInput = np.array(textInput) sentence = ' '.join(sentence.split(' ')[1:-1]).upper() + '.' return sentence
def train_generator(df, batch_size): while True: df = df.sample(frac=1, random_state=randint(11, 99)).reset_index(drop=True) for start in range(0, df.shape[0], batch_size): end = min(start + batch_size, df.shape[0]) sub_df = df.iloc[start:end, :] x_batch = [] y_batch = [] for index, row in sub_df.iterrows(): img_path = row[ftype] img = cv2.imread(img_path) img = cv2.resize(img, (SIZE, SIZE), interpolation=cv2.INTER_CUBIC) if random.random() < 0.5: img = np.fliplr(img) img = image.img_to_array(img) img = preprocess_input(img) x_batch.append(img) y_batch.append( to_categorical(row['class_id'], num_classes=NUMBER_OF_CLASSES)) yield np.array(x_batch), np.array(y_batch)
def test_pipeline(self): """ Pipeline should provide correct function composition """ img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg')) xcpt_model = Xception(weights="imagenet") stages = [('spimage', gfac.buildSpImageConverter(SparkMode.RGB_FLOAT32)), ('xception', GraphFunction.fromKeras(xcpt_model))] piped_model = GraphFunction.fromList(stages) for fpath in img_fpaths: target_size = tuple(xcpt_model.input.shape.as_list()[1:-1]) img = load_img(fpath, target_size=target_size) img_arr = np.expand_dims(img_to_array(img), axis=0) img_input = xcpt.preprocess_input(img_arr) preds_ref = xcpt_model.predict(img_input) spimg_input_dict = imageArrayToStruct(img_input).asDict() spimg_input_dict['data'] = bytes(spimg_input_dict['data']) with IsolatedSession() as issn: # Need blank import scope name so that spimg fields match the input names feeds, fetches = issn.importGraphFunction(piped_model, prefix="") feed_dict = dict((tnsr, spimg_input_dict[tfx.op_name(issn.graph, tnsr)]) for tnsr in feeds) preds_tgt = issn.run(fetches[0], feed_dict=feed_dict) # Uncomment the line below to see the graph # tfx.write_visualization_html(issn.graph, # NamedTemporaryFile(prefix="gdef", suffix=".html").name) self.assertTrue(np.all(preds_tgt == preds_ref))
def data_generator_test(data, n): # 样本生成器,节省内存 while True: batch = np.array([data[n]]) x, y = [], [] for img in batch: im = misc.imread(img) im = im[:, :, :3] im = misc.imresize(im, img_size) x.append(im) # 读取resize图片,再存进x列表 y_list = [] real_num = str(img).split('\\')[1].split('.')[0] for i in real_num: i = i.upper() if 48 <= ord(i) <= 57: y_list.append(ord(i) - 48) elif 65 <= ord(i) <= 90: y_list.append(ord(i) - 55) elif 97 <= ord(i) <= 122: y_list.append(ord(i) - 61) else: print(real_num) y.append(y_list) y.append( y_list) # 把验证码标签添加到y列表,ord(i)-ord('a')把对应字母转化为数字a=0,b=1……z=26 # print('real_1:',img[-8:-4]) x = preprocess_input( np.array(x).astype(float)) # 原先是dtype=uint8转成一个纯数字的array y = np.array(y) yield x, [y[:, i] for i in range(4)]
def main(args): # create model model = load_model(args.model) # load class names classes = [] with open(args.classes, 'r') as f: classes = list(map(lambda x: x.strip(), f.readlines())) # load an input image while (str(input("Press any key to continue, 'n' to exit:")) != 'n'): img = str(input("Enter a image path:")) # img = image.load_img(args.image, target_size=(299, 299)) img = image.load_img(img, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # predict pred = model.predict(x)[0] result = [(classes[i], float(pred[i]) * 100.0) for i in range(len(pred))] result.sort(reverse=True, key=lambda x: x[1]) for i in range(min(args.top_n, len(classes))): (class_name, prob) = result[i] print("Top %d ====================" % (i + 1)) print("Class name: %s" % (class_name)) print("Probability: %.2f%%" % (prob))
def predictCutPic11(picPath, cutH, cutW, cutStep, model, padding=False, paddingSize=160): #用于分割图片的预测 img = cv2.imread(picPath) if padding == True: img = cv2.copyMakeBorder(img, paddingSize, paddingSize, paddingSize, paddingSize, cv2.BORDER_CONSTANT, value=0) h, w, c = img.shape cutNum = int((h - cutH + cutStep) / cutStep) * int( (w - cutW + cutStep) / cutStep) cutImgBatch = np.zeros((cutNum, cutH, cutW, 3)) i = 0 for y1 in range(0, h - cutH + cutStep, cutStep): for x1 in range(0, w - cutW + cutStep, cutStep): x2, y2 = x1 + cutW, y1 + cutH cutImg = img[y1:y2, x1:x2] #cutImgBatch[i] = cutImg/255. cutImg = cutImg.astype(np.float32) cutImgBatch[i] = preprocess_input(cutImg) i += 1 pArray = model.predict(cutImgBatch) return pArray
def identify(filepath): img = read_img(filepath, (299, 299)) x = xception.preprocess_input(np.expand_dims(img.copy(), axis=0)) x_bottleneck = xception_bottleneck.predict(x, verbose=1) pred = logreg.predict(x_bottleneck) return CATEGORIES[pred[0]]
def get_np_image(df, target_size = (299,299,3)): img = image.load_img("data/"+"test/"+df, target_size=target_size) x = image.img_to_array(img) x = preprocess_input(x) return(x)
def predict_breed(img_path): """Predict the breed of a dog in a given image using a trained CNN Parameters ---------- img_path : str The path to the image to be classified Returns ------- str The predicted breed of the dog in the image """ with open("utility_files/dog_names.pickle", "rb") as f: dog_names = pickle.load(f) xception_model = load_xception_model() img = xception.preprocess_input(path_to_tensor(img_path)) bottleneck_feature = xception.Xception( weights="imagenet", include_top=False ).predict(img) with xception_graph.as_default(): predicted_vector = xception_model.predict(bottleneck_feature) prediction = dog_names[np.argmax(predicted_vector)] return prediction
def data_generator(data, classes, steps_per_epoch, batch_size, image_size, augment=False): """ Data generator data: List of filenames and their labels """ X, y = [], [] data = list(np.copy(data)) random.shuffle(data) total_steps_elapsed = 0 while True: iter_cycle_obj = itertools.cycle(data) for im_filename, label in iter_cycle_obj: # Get image im = io.imread(im_filename) if augment: im = augment_im(im) im = transform.resize(im, output_shape=image_size) # Get label label = class2dummy(classes, label) # Append X, y X.append(im) y.append(label) # Check if batch_size is reached if len(y) > batch_size - 1: X, y = np.array(X), np.array(y) X = xception.preprocess_input(X * 255.0) yield (X, y) X, y = [], [] total_steps_elapsed += 1 # Check total_steps_elapsed (end of epoch) if total_steps_elapsed > steps_per_epoch: random.shuffle(data) total_steps_elapsed = 0 break
def aug(X): seq = get_seq() X = [X] X = seq.augment_images(X) X = np.asarray(X[0]) X = preprocess_input(X.astype(np.float32)) return X
def pp_image(img): img = image.load_img('pic.png', target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return np.asarray(x)
def xception_process(image_path, k=5, threshold=None): """ Extracts returns the k top predictions. :param image_path: :param k: :return: """ img = image.load_img(image_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = xception_model.predict(x) preds = decode_predictions(preds, top=k)[0] if threshold is not None: preds = [ dict(desc=p[1], prob=float(p[2])) for p in preds if p[2] >= threshold ] else: preds = [dict(desc=p[1], prob=float(p[2])) for p in preds] # decode the results into a list of tuples (class, description, probability) # (one such list for each sample in the batch) return preds
def predictCutPic11(picPath, cutH, cutW, cutStep, model, padding=False, paddingSize=160): #用于分割图片的预测 img = cv2.imread(picPath) if padding == True: img = cv2.copyMakeBorder(img, paddingSize, paddingSize, paddingSize, paddingSize, cv2.BORDER_CONSTANT, value=0) h, w, c = img.shape xList = gen_cutXY_list(w, cutW, cutStep) yList = gen_cutXY_list(h, cutH, cutStep) cutNum = len(xList) * len(yList) cutImgBatch = np.zeros((cutNum, cutH, cutW, 3)) i = 0 for y1 in yList: for x1 in xList: x2, y2 = x1 + cutW, y1 + cutH cutImg = img[y1:y2, x1:x2] #cutImgBatch[i] = cutImg/255. cutImg = cutImg.astype(np.float32) cutImgBatch[i] = preprocess_input(cutImg) i += 1 pArray = model.predict(cutImgBatch) return pArray
def predict(): songs = Song.query.all() prefs = songs[0].pref prefs = prefs.split(",") prefs = random.choice(prefs) with open("D:\ReactStuff\FeelTheBeat\Python\model_architecture.json", "r") as f: model = model_from_json(f.read()) model.load_weights("D:\ReactStuff\FeelTheBeat\Python\model_weights.h5") image = Image.open("D:\ReactStuff\FeelTheBeat\Python\predict.png") new_image = Image.new("RGB", (299, 299), (255, 255, 255)) new_image.paste(image, mask=image.split()[3]) #image = np.array(image.resize((299, 299))) img = np.array(new_image).reshape(1, 299, 299, 3) img = preprocess_input(img) prediction = model.predict(img) prediction = np.argmax(prediction) if prediction == 0: emotion = "Angry" elif prediction == 1: emotion = "Calm" elif prediction == 2: emotion = "Happy" else: emotion = "Sad" youtube = build("youtube", "v3", developerKey="AIzaSyCXBY-aPwP4eFdaIKzdbDtZwCKq3zBdxrY") req = youtube.search().list(q=f"{prefs} {emotion} song", part="snippet", type="video") res = req.execute() songs = {} for i, item in enumerate(res["items"]): songs[f"songName{i+1}"] = item["snippet"]["title"] songs[ f"songLink{i+1}"] = f"https://www.youtube.com/watch?v={item['id'][f'videoId']}" return jsonify({ "songName1": songs["songName1"], "songName2": songs["songName2"], "songName3": songs["songName3"], "songName4": songs["songName4"], "songName5": songs["songName5"], "songLink1": songs["songLink1"], "songLink2": songs["songLink2"], "songLink3": songs["songLink3"], "songLink4": songs["songLink4"], "songLink5": songs["songLink5"], "emotion": emotion, })
from keras.layers import Conv2D from keras.layers import SeparableConv2D from keras.layers import MaxPooling2D from keras.layers import GlobalAveragePooling2D from keras.layers import GlobalMaxPooling2D from keras.engine.topology import get_source_inputs from keras.utils.data_utils import get_file from keras import backend as K from keras.applications.imagenet_utils import decode_predictions from keras.applications.imagenet_utils import _obtain_input_shape from keras.applications import Xception from keras.applications.xception import preprocess_input TF_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels.h5' TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels_notop.h5' if __name__ == '__main__': model = Xception(include_top=True, weights='imagenet') img_path = 'data\\dogscats\\train\\cats\\cat.10013.jpg' img = image.load_img(img_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) print('Input image shape:', x.shape) preds = model.predict(x) print(np.argmax(preds)) print('Predicted:', decode_predictions(preds, 1)) # ('n02123394', 'Persian_cat', 0.91428012)
usePostprocessing = len(sys.argv) > 2 if usePostprocessing: path2Scaler = sys.argv[2] with open(path2Scaler) as f: scaler = pickle.load(f) postprocess = lambda x: scaler.inverse_transform(x) else: postprocess = None #img_dir = '/home/daiver/R3DS/Data/Render2ShapeRegression/blendshape_data/' img_dir = '/home/daiver/R3DS/Data/Render2ShapeRegression/NeutralFacesDataset/' model = keras.models.load_model(path2Model) from keras.applications.xception import preprocess_input preprocess = lambda x: preprocess_input(x.astype(np.float32).reshape(-1, x.shape[0], x.shape[1], x.shape[2]))[0] preprocess = lambda x: cv2.cvtColor(x, cv2.COLOR_BGR2GRAY).reshape((x.shape[0], x.shape[1], 1)) train_dir = img_dir + "train/" imgs_train, y_train, names_train = loadDataMultiViewByTargetFile(train_dir, preprocess) test_dir = img_dir + "test/" imgs_test, y_test, names_test = loadDataMultiViewByTargetFile(test_dir, preprocess) if usePostprocessing: y_train = scaler.transform(y_train) y_test = scaler.transform(y_test) score_train = model.evaluate(imgs_train, y_train, verbose=0) score_test = model.evaluate(imgs_test, y_test, verbose=0)
def preprocess(self, inputImage): return xception.preprocess_input(inputImage)
def preprocess(self, inputImage): # Keras expects RGB order return xception.preprocess_input(_reverseChannels(inputImage))
def rgb_feature_net_x(input): # Xception feature extractor batch_size, img_height, img_width, img_channel = input.get_shape().as_list() # with tf.variable_scope('xception_model'): # base_model= xcep.Xception(include_top=False, weights=None, # input_shape=(img_height, img_width, img_channel )) # # print(base_model.summary()) # # base_model_input = base_model.get_layer('input_2').input # base_model_output = base_model.get_layer('block12_sepconv3_bn').output # # print(model.summary()) with tf.variable_scope('preprocess'): block = maxpool(input, kernel_size=(2,2), stride=[1,2,2,1], padding='SAME', name='4' ) block = xcep.preprocess_input(block) with tf.variable_scope('feature_extract'): # keras/applications/xception.py print('build Xception') x = Conv2D(32, (3, 3), strides=(2, 2), use_bias=False, name='block1_conv1')(block) x = BatchNormalization(name='block1_conv1_bn')(x) x = Activation('relu', name='block1_conv1_act')(x) x = Conv2D(64, (3, 3), use_bias=False, name='block1_conv2')(x) x = BatchNormalization(name='block1_conv2_bn')(x) x = Activation('relu', name='block1_conv2_act')(x) residual = Conv2D(128, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv1')(x) x = BatchNormalization(name='block2_sepconv1_bn')(x) x = Activation('relu', name='block2_sepconv2_act')(x) x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv2')(x) x = BatchNormalization(name='block2_sepconv2_bn')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block2_pool')(x) x = layers.add([x, residual]) residual = Conv2D(256, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) x = Activation('relu', name='block3_sepconv1_act')(x) x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv1')(x) x = BatchNormalization(name='block3_sepconv1_bn')(x) x = Activation('relu', name='block3_sepconv2_act')(x) x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv2')(x) x = BatchNormalization(name='block3_sepconv2_bn')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block3_pool')(x) x = layers.add([x, residual]) residual = Conv2D(728, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) x = Activation('relu', name='block4_sepconv1_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv1')(x) x = BatchNormalization(name='block4_sepconv1_bn')(x) x = Activation('relu', name='block4_sepconv2_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv2')(x) x = BatchNormalization(name='block4_sepconv2_bn')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block4_pool')(x) x = layers.add([x, residual]) i = None for i in range(7): residual = x prefix = 'block' + str(i + 5) x = Activation('relu', name=prefix + '_sepconv1_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv1')(x) x = BatchNormalization(name=prefix + '_sepconv1_bn')(x) x = Activation('relu', name=prefix + '_sepconv2_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv2')(x) x = BatchNormalization(name=prefix + '_sepconv2_bn')(x) x = Activation('relu', name=prefix + '_sepconv3_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv3')(x) x = BatchNormalization(name=prefix + '_sepconv3_bn')(x) x = layers.add([x, residual]) i += 1 prefix = 'block' + str(i + 5) x = Activation('relu', name=prefix + '_sepconv1_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv1')(x) x = BatchNormalization(name=prefix + '_sepconv1_bn')(x) x = Activation('relu', name=prefix + '_sepconv2_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv2')(x) x = BatchNormalization(name=prefix + '_sepconv2_bn')(x) x = Activation('relu', name=prefix + '_sepconv3_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv3')(x) x = BatchNormalization(name=prefix + '_sepconv3_bn')(x) block = x block = conv2d_bn_relu(block, num_kernels=128, kernel_size=(1, 1), stride=[1, 1, 1, 1], padding='SAME', name='conv') stride = 32 feature = block print ('rgb : scale=%f, stride=%d'%(1./stride, stride)) return feature, stride