def perform_inference(exec_net, request_type, input_image, input_shape): ''' Performs inference on an input image, given an ExecutableNetwork ''' # Get input image image = cv2.imread(input_image) # Extract the input shape n, c, h, w = input_shape # Preprocess it (applies for the IRs from the Pre-Trained Models lesson) preprocessed_image = preprocessing(image, h, w) # Get the input blob for the inference request input_blob = next(iter(exec_net.inputs)) # Perform either synchronous or asynchronous inference request_type = request_type.lower() if request_type == 'a': output = async_inference(exec_net, input_blob, preprocessed_image) elif request_type == 's': output = sync_inference(exec_net, input_blob, preprocessed_image) else: print("Unknown inference request type, should be 'A' or 'S'.") exit(1) # Return the exec_net for testing purposes return output
def main(): # Preprocessing # Reading the crawled data df = pd.read_csv('magazine_products.csv', usecols=['Titulo', 'category']) # Creating the dataset dataset = [] for category, title in zip(df['category'], df['Titulo']): # classes only geladeira/refrigerador and lavadora # replace frigobar with geladeira/refrigerador category = category.replace('/ ', '/').split(' ')[0].lower() if (category not in ('acessórios')): if (category in ('frigobar')): category = 'geladeira/refrigerador' dataset.append({'categoria': category, 'titulo': title}) docs, words, classes = helpers.preprocessing(dataset) X, y = helpers.bag_of_words(docs, words, classes) # Shuffling the data so that the model doesn't overfit random.shuffle(X) random.shuffle(y) X = np.array(X) y = np.array(y) # TensorFlow for building and training the classifier # Clearing any data from previous graphs tf.reset_default_graph() # Building the NN nn = tflearn.input_data(shape=[None, len(X[0])]) nn = tflearn.fully_connected(nn, 10, activation='relu') nn = tflearn.dropout(nn, 0.2) nn = tflearn.fully_connected(nn, 10, activation='sigmoid') # Dropout so that the neural network don't memorize the data and prevent overfitting nn = tflearn.fully_connected(nn, 10, activation='relu') # Output layer, softmax as activation function (best for classification) nn = tflearn.fully_connected(nn, len(y[0]), activation='softmax') # Applys linear or logistic regression to the input nn = tflearn.regression(nn, optimizer='adam', loss='categorical_crossentropy') # Defining the deep neural network model and setting up the tensorboard model = tflearn.DNN(nn, tensorboard_dir='data/tflearn_logs') # Training using adam as optmizer and crossentropy loss function # Using 70% for training and 30% for validation(testing) model.fit(X, y, validation_set=0.3, batch_size=12, n_epoch=500, show_metric=True) # Saving the trained model that ill be used for testing later model.save('model/model.tflearn')
if args.handle_gpu: helpers.handle_gpu_compatibility() batch_size = 1 # If you have trained faster rcnn model you can load weights from faster rcnn model load_weights_from_frcnn = False hyper_params = helpers.get_hyper_params(nms_topn=10) VOC_test_data, VOC_info = helpers.get_dataset("voc/2007", "test") labels = helpers.get_labels(VOC_info) # We add 1 class for background hyper_params["total_labels"] = len(labels) + 1 # If you want to use different dataset and don't know max height and width values # You can use calculate_max_height_width method in helpers max_height, max_width = helpers.VOC["max_height"], helpers.VOC["max_width"] VOC_test_data = VOC_test_data.map(lambda x : helpers.preprocessing(x, max_height, max_width)) padded_shapes, padding_values = helpers.get_padded_batch_params() VOC_test_data = VOC_test_data.padded_batch(batch_size, padded_shapes=padded_shapes, padding_values=padding_values) base_model = VGG16(include_top=False) if hyper_params["stride"] == 16: base_model = Sequential(base_model.layers[:-1]) rpn_model = rpn.get_model(base_model, hyper_params) frcnn_model_path = helpers.get_model_path("frcnn", hyper_params["stride"]) rpn_model_path = helpers.get_model_path("rpn", hyper_params["stride"]) model_path = frcnn_model_path if load_weights_from_frcnn else rpn_model_path rpn_model.load_weights(model_path, by_name=True) for image_data in VOC_test_data:
load_weights = False hyper_params = helpers.get_hyper_params() VOC_train_data, VOC_info = helpers.get_dataset("voc/2007", "train+validation") VOC_val_data, _ = helpers.get_dataset("voc/2007", "test") VOC_train_total_items = helpers.get_total_item_size(VOC_info, "train+validation") VOC_val_total_items = helpers.get_total_item_size(VOC_info, "test") step_size_train = helpers.get_step_size(VOC_train_total_items, batch_size) step_size_val = helpers.get_step_size(VOC_val_total_items, batch_size) labels = helpers.get_labels(VOC_info) # We add 1 class for background hyper_params["total_labels"] = len(labels) + 1 # If you want to use different dataset and don't know max height and width values # You can use calculate_max_height_width method in helpers max_height, max_width = helpers.VOC["max_height"], helpers.VOC["max_width"] VOC_train_data = VOC_train_data.map(lambda x : helpers.preprocessing(x, max_height, max_width)) VOC_val_data = VOC_val_data.map(lambda x : helpers.preprocessing(x, max_height, max_width)) padded_shapes, padding_values = helpers.get_padded_batch_params() VOC_train_data = VOC_train_data.padded_batch(batch_size, padded_shapes=padded_shapes, padding_values=padding_values) VOC_val_data = VOC_val_data.padded_batch(batch_size, padded_shapes=padded_shapes, padding_values=padding_values) frcnn_train_feed = faster_rcnn.generator(VOC_train_data, hyper_params, preprocess_input) frcnn_val_feed = faster_rcnn.generator(VOC_val_data, hyper_params, preprocess_input) base_model = VGG16(include_top=False) if hyper_params["stride"] == 16: base_model = Sequential(base_model.layers[:-1]) # rpn_model = rpn.get_model(base_model, hyper_params) frcnn_model = faster_rcnn.get_model(base_model, rpn_model, hyper_params)
helpers.handle_gpu_compatibility() batch_size = 1 # If you have trained faster rcnn model you can load weights from faster rcnn model load_weights_from_frcnn = False hyper_params = helpers.get_hyper_params(nms_topn=10) VOC_test_data, VOC_info = helpers.get_VOC_data("test") labels = helpers.get_labels(VOC_info) # We add 1 class for background hyper_params["total_labels"] = len(labels) + 1 # If you want to use different dataset and don't know max height and width values # You can use calculate_max_height_width method in helpers max_height, max_width = helpers.VOC["max_height"], helpers.VOC["max_width"] VOC_test_data = VOC_test_data.map( lambda x: helpers.preprocessing(x, max_height, max_width)) padded_shapes, padding_values = helpers.get_padded_batch_params() VOC_test_data = VOC_test_data.padded_batch(batch_size, padded_shapes=padded_shapes, padding_values=padding_values) base_model = VGG16(include_top=False) if hyper_params["stride"] == 16: base_model = Sequential(base_model.layers[:-1]) rpn_model = rpn.get_model(base_model, hyper_params) frcnn_model_path = helpers.get_model_path("frcnn", hyper_params["stride"]) rpn_model_path = helpers.get_model_path("rpn", hyper_params["stride"]) model_path = frcnn_model_path if load_weights_from_frcnn else rpn_model_path rpn_model.load_weights(model_path, by_name=True)