Ejemplo n.º 1
0
def predict():
    if request.method == 'GET':
        try:
            url = request.args.get('q')
            app.logger.debug('url provided - %s', url)
            input_tensor = transform_image(read_file(url=url))
            values, indices = get_topk(input_tensor)
            results = render_prediction(values, indices)
            return jsonify(results=results)

        except:
            app.logger.debug("Error: %s", traceback.print_exc())
            return jsonify("invalid image url")

    elif request.method == 'POST':
        try:
            file = request.files['file']
            app.logger.debug('file uploaded - %s', file)
            url = request.form.get("url", None)
            app.logger.debug('url provided - %s', url)

            input_tensor = transform_image(read_file(upload=file, url=url))
            values, indices = get_topk(input_tensor)
            results = render_prediction(values, indices)
            return jsonify(results=results)

        except:
            app.logger.debug("Error: %s", traceback.print_exc())
            return jsonify("invalid image")

    else:
        app.logger.debug("Error: %s", traceback.print_exc())
        return jsonify('invalid request')
def run_inference():

    os.system(
        'fswebcam -r 1024x768 --no-banner --scale 224x224 output.jpg -S 7 --save /home/pi/Photos/std.jpg'
    )  # uses Fswebcam to take picture
    image = Image.open('output.jpg')
    #data = np.array(image,dtype='float64')
    #data=data1.reshape((1,data1.shape[2],data1.shape[0],data1.shape[1]))

    #np.save( 'flamingo.npy', data)

    image_data = utils.transform_image(image)
    print(image_data)
    flattened_data = image_data.astype(np.float32).flatten()
    #np.save( 'puppi.npy',flattened_data)
    print("Start Prinring Flattern")
    print(flattened_data)
    #run_inference(image_data)
    #time.sleep(15) # this line creates a 15 second delay before repeating the loop

    model_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                              '../model-rasp3b')
    batch_size = 1
    channels = 3
    height = width = 224
    input_shape = {'input0': [batch_size, channels, height, width]}
    classes = 1000
    output_shape = [batch_size, classes]
    device = 'cpu'
    model = DLRModel(model_path, input_shape, output_shape, device)

    synset_path = os.path.join(model_path, 'imagenet1000_clsidx_to_labels.txt')
    with open(synset_path, 'r') as f:
        synset = eval(f.read())

    #image = np.load(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dog.npy')).astype(np.float32)
    #input_data = {'data': image_data}

    # Predict
    out = model.run({'input0': flattened_data}).squeeze()
    top1 = np.argmax(out)
    prob = np.max(out)
    print("Class: %s, probability: %f" % (synset[top1], prob))

    for rep in range(4):
        t1 = current_milli_time()
        out = model.run({'input0': flattened_data}).squeeze()
        t2 = current_milli_time()

        logging.debug('done m.run(), time (ms): {}'.format(t2 - t1))

        top1 = np.argmax(out[0])
        logging.debug('Inference result: {}, {}'.format(top1, synset[top1]))

    import resource
    logging.debug(
        "peak memory usage (bytes on OS X, kilobytes on Linux) {}".format(
            resource.getrusage(resource.RUSAGE_SELF).ru_maxrss))

    return {'synset_id': top1, 'prediction': synset[top1], 'time': t2 - t1}
Ejemplo n.º 3
0
 def image(self):
     """
     Returns original image rescaled, rotated, shifted and amplified.
     """
     print("Calculating image for parameters: {}".format(self.p))
     return transform_image(self._image, self.p[0], self.p[1], self.p[2],
                            self.p[3], self.p[4])
Ejemplo n.º 4
0
def _process_image(filename, tmp_name, imgTransformationCount=5):
    global AUGMENT

    model_file = file_io.FileIO(filename, mode='rb')
    temp_model_location = './%s.png' % tmp_name
    temp_model_file = open(temp_model_location, 'wb')
    temp_model_file.write(model_file.read())
    temp_model_file.close()

    image_data = cv2.imread(temp_model_location)
    decoded = cv2.imencode('.png', image_data)[1].tostring()
    height, width, _ = image_data.shape

    decoded_strings = list()
    decoded_strings.append(decoded)

    if AUGMENT:
        # Generate more images per each original image by applying random transformations.
        for i in range(imgTransformationCount):
            im_trans_data = utils.transform_image(image_data)
            trans_decoded = cv2.imencode('.png', im_trans_data)[1].tostring()
            decoded_strings.append(trans_decoded)

    # Return the list of decoded strings along with height and weight of each image.
    # Each transformed image is having the same shape as the original image.
    return decoded_strings, height, width
Ejemplo n.º 5
0
def predict_from_cam():
    r"""
    Predict with the photo taken from your pi camera.
    """
    #send_mqtt_message("Taking a photo...")
    my_camera = camera.Camera()
    image = Image.open(my_camera.capture_image())
    image_data = utils.transform_image(image)

    #send_mqtt_message("Start predicting...")
    predict(image_data)
Ejemplo n.º 6
0
def get_prediction(image_bytes):
    try:
        img = transform_image(image_bytes=image_bytes)
        mask = model.forward(img)
    except Exception:
        return 0, 'error'

    with torch.no_grad():
        y = torch.sigmoid(mask)
        mask = y.squeeze().cpu().numpy()

    return mask > .6
Ejemplo n.º 7
0
def main():

    graph = tf.Graph()
    with graph.as_default():
        with gfile.FastGFile(utils.PATH_TO_MERGED_GRAPH, 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            tf.import_graph_def(graph_def, name='')

    # input tensor
    image_input = graph.get_tensor_by_name('image_input:0')

    # output tensors
    tensors = [
        "class/final_result", "detect/detection_classes",
        "detect/num_detections", "detect/detection_scores",
        "detect/detection_boxes"
    ]
    tensor_dict = {out: '%s:0' % out for out in tensors}
    tensor_dict = {
        out: graph.get_tensor_by_name(name)
        for out, name in tensor_dict.items()
    }

    # examples
    start = 1003
    examples_number = 500
    examples = pd.read_csv(utils.LABELLING_OUTPUT_PATH)
    examples = examples.sample(frac=1).reset_index(drop=True)
    examples = examples.iloc[start:start + examples_number]

    # categories = pd.read_csv('categories.csv')
    results = []

    with tf.Session(graph=graph) as sess:

        for row_id, row in examples.iterrows():
            image = utils.get_images(pd.Series(row['path_to_image']))[0]
            image = utils.transform_image(image)
            result = sess.run(tensor_dict, {image_input: image})
            result = {key: value[0] for key, value in result.items()}

            classification: np.ndarray = result['class/final_result']
            class_max = np.argmax(classification)
            results.append((class_max, row['label']))
            print(row_id, class_max, row['label'])

    results = pd.DataFrame(results, columns=['result', 'all_label'])
    results.to_csv('result2.csv')
    # results = pd.merge(results, categories)

    print(results)
Ejemplo n.º 8
0
def main():

    graph = tf.Graph()
    with graph.as_default():
        with gfile.FastGFile(utils.PATH_TO_MERGED_GRAPH, 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            tf.import_graph_def(graph_def, name='')

    # input tensor
    image_input = graph.get_tensor_by_name('image_input:0')

    # output tensors
    tensors = [
        "class/final_result", "detect/detection_classes",
        "detect/num_detections", "detect/detection_scores",
        "detect/detection_boxes"
    ]
    tensor_dict = {out: '%s:0' % out for out in tensors}
    tensor_dict = {
        out: graph.get_tensor_by_name(name)
        for out, name in tensor_dict.items()
    }

    # examples
    examples_number = 32
    examples = pd.read_csv(utils.LABELLING_OUTPUT_PATH)
    examples = examples.sample(frac=1).reset_index(drop=True)
    examples = examples.iloc[:examples_number]
    examples['image'] = utils.get_images(examples['path_to_image'])

    # display
    utils.show_images(examples, utils.MAPPER_CLASSIFICATION)

    with tf.Session(graph=graph) as sess:

        for _, row in examples.iterrows():
            image = utils.transform_image(row['image'])
            result = sess.run(tensor_dict, {image_input: image})
            result = {key: value[0] for key, value in result.items()}

            detections = utils.construct_detection_df(result)
            classification: np.ndarray = result['class/final_result']
            classification = utils.decode_classification(classification)
            print(detections)
            print(classification)

            image = utils.visualize_boxes_and_labels(row['image'], detections)
            plt.imshow(image)
            plt.show()
Ejemplo n.º 9
0
def ajax_test():

    _, imgstring = request.data.decode("utf-8").split(',')
    img = io.BytesIO(base64.b64decode(imgstring))
    img = utils.transform_image(img, settings.IMAGE_WIDTH, settings.IMAGE_HEIGHT, True, 'test.png')

    K.clear_session()

    the_model = load_model(settings.MODEL_FILE)

    result = the_model.predict(np.array([img]))
    prediction = np.argmax(result)
    probability = result[0][prediction]

    return 'It\'s a %d (%.2f)' % (prediction, probability)
Ejemplo n.º 10
0
def predict_from_cam():
    r"""
    Predict with the photo taken from your pi camera.
    """
    send_mqtt_message("Taking a photo...")
    my_camera = camera.Camera()
    imagebinary = my_camera.capture_image()
    image = Image.open(imagebinary)
    image_data = utils.transform_image(image)

    send_mqtt_message("Start predicting...")
    max_class, max_score = predict(image_data)

    # DataEncode
    image64 = base64.b64encode(imagebinary.getvalue())
    image_str = image64.decode("utf-8")

    send_prediction_results(image_str, max_class, float(max_score))

    return imagebinary
Ejemplo n.º 11
0
model.save('deep_models/inception_west_train.h5')

model = load_model('deep_models/inception_west_train.h5')

#preparing data for predictions
labels_index = {0: "non-crater", 1: "crater"}
X_eval = list()
y_eval = list()

# crater part
files = os.listdir('./crater_data/test_set/crater')
files.sort()

for i in range(0, len(files) - 1):
    X_eval.append(
        transform_image('./crater_data/test_set/crater/' + files[i + 1],
                        input_shape))
    y_eval.append(1)

# non-crater part
files = os.listdir('./crater_data/test_set/non-crater')
files.sort()

for i in range(0, len(files) - 1):
    X_eval.append(
        transform_image('./crater_data/test_set/non-crater/' + files[i + 1],
                        input_shape))
    y_eval.append(0)

# stacking the arrays
X_eval = np.vstack(X_eval)
Ejemplo n.º 12
0
skp_right = sift.detect(im_right)
skp_left, sd_left = sift.compute(im_left, skp_left)
skp_right, sd_right = sift.compute(im_right, skp_right)

# Plot the keypoints on the image
keypoints_left = draw_keypoints(im_left, skp_left)
plot_image(keypoints_left, 'Keypoints on Left Image')
keypoints_right = draw_keypoints(im_right, skp_right)
plot_image(keypoints_right, 'Keypoints on Right Image')

# Adjust the descriptors to be of equal sizes
if sd_left.size < sd_right.size:
    sd_right = sd_right[0:sd_left.shape[0], 0:sd_left.shape[1]]
elif sd_left.size >= sd_right.size:
    sd_left = sd_left[0:sd_right.shape[0], 0:sd_right.shape[1]]

# Identify the matched pairs of points
match_points = create_matched_pairs(sd_left, sd_right, 0.5)

# Plot the matched pairs on the image
large_image = match_image_points(im_left, im_right, skp_left, skp_right, match_points, True)
plot_image(large_image, 'Matched SIFT Points')

# Transform the right image to be the same plane as left image
transformed = transform_image(im_left, im_right, skp_left, skp_right, match_points)
plot_image(transformed, 'Transformed Image')

# Stitch the left and right image to create the panorama
panorama = make_panorama(transformed, im_left)
plot_image(panorama, 'Stitched Image')
Ejemplo n.º 13
0
#preparing data for predictions

size = (64, 64)
X_eval = list()
y_eval = list()
#test_name = 'challenging_test_set'
test_name = 'test_set'

# crater part
files = os.listdir('./crater_data/' + test_name + '/crater')
files.sort()

for i in range(0, len(files) - 1):
    X_eval.append(
        transform_image(
            './crater_data/' + test_name + '/crater/' + files[i + 1], size))
    y_eval.append(1)

# non-crater part
files = os.listdir('./crater_data/' + test_name + '/non-crater')
files.sort()

for i in range(0, len(files) - 1):
    X_eval.append(
        transform_image(
            './crater_data/' + test_name + '/non-crater/' + files[i + 1],
            size))
    y_eval.append(0)

# stacking the arrays
X_eval = np.vstack(X_eval)
Ejemplo n.º 14
0
def train_pix2pix(pathdataset: str,
                  pathmodel: str,
                  pathlogs: str,
                  epochs: int = 150,
                  learningrate: float = 2e-4,
                  batchsize: int = 1) -> str:
    """
  Train Pix2Pix.

  Args:
    pathdataset      (string) : Full path name to the training dataset (TFRecords file)
    pathmodel        (string) : Full path name to the Pix2Pix model checkpoint
    pathlogs         (string) : Full path name to the Tensorboard logs directory
    epochs           (int)    : Number of training epochs
    learningrate     (float)  : Learning rate (for generator and discriminator)
    batchsize        (int)    : Batch size

  Returns:
    model            (string) : Full path name to the Pix2Pix model checkpoint
  """

    # ------------------------------
    # In order to be able to convert
    #  a Python Function directly
    #  into a Kubeflow component,
    #   we need to move the python
    #  includes inside that python
    #  function.
    # ------------------------------
    import tensorflow as tf
    import json
    import time

    # The below tag comment is used by a tool script from this project to automatically nest
    # the python code of the imports function tagged KFP-NEST-IMPORT, just right after this tag.
    # This is only usefull when using the SDK's kfp.components.func_to_container_op method,
    # which allows to convert a Python function to a pipeline component and returns a factory function
    #
    #KFP-NEST-HERE

    # ------------------------------
    #  Define some hyperparameters
    #   (Not managed as Kubeflow
    #      pipeline parameters)
    # ------------------------------
    BETA_1 = 0.5  # Exponential decay rate for the 1st moment estimates

    # ------------------------------
    #   Get ready for Training
    # ------------------------------

    # Build the Pix2Pix GAN
    generator = Generator()
    discriminator = Discriminator()

    # Set the Optimizers
    generator_optimizer = tf.keras.optimizers.Adam(learningrate, BETA_1)
    discriminator_optimizer = tf.keras.optimizers.Adam(learningrate, BETA_1)

    # Configure the model checkpoints saving
    checkpoint = tf.train.Checkpoint(
        generator_optimizer=generator_optimizer,
        discriminator_optimizer=discriminator_optimizer,
        generator=generator,
        discriminator=discriminator)
    manager = tf.train.CheckpointManager(checkpoint,
                                         directory=pathmodel,
                                         max_to_keep=1,
                                         checkpoint_name='ckpt')
    _ = checkpoint.restore(manager.latest_checkpoint)

    if manager.latest_checkpoint:
        print("Model restored from {}".format(manager.latest_checkpoint))
    else:
        print("Initializing from scratch.")

    # Configure a summary writer to collect Tensorboard logs
    summary_writer = tf.summary.create_file_writer(pathlogs)

    # Enable Kubeflow Pipelines UI built-in support for Tensorboard
    try:
        metadata = {
            'outputs': [{
                'type': 'tensorboard',
                'source': pathlogs,
            }]
        }

        # This works only inside Docker containers
        with open('/mlpipeline-ui-metadata.json', 'w') as f:
            json.dump(metadata, f)

    except PermissionError:
        pass

    # ------------------------------
    #       One step Training
    #    nested Helper function
    # ------------------------------
    @tf.function  # Compile function into a graph for faster execution
    def train_step(input_image, target):
        '''
    Perform one training step

    Args:
      input_image   : Input image
      target        : Output image (ground thruth)

    Returns:
      gen_loss    : Generator loss
      disc_loss   : Dicriminator loss

    '''
        with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:

            # Compute the Generator output
            gen_output = generator(input_image, training=True)

            # Compute the Discriminator output for real and generated inputs
            disc_real_output = discriminator([input_image, target],
                                             training=True)
            disc_generated_output = discriminator([input_image, gen_output],
                                                  training=True)

            # Computes the Generator and Discriminator losses
            gen_loss = generator_loss(disc_generated_output, gen_output,
                                      target)
            disc_loss = discriminator_loss(disc_real_output,
                                           disc_generated_output)

        # Apply Gradient Descent
        generator_gradients = gen_tape.gradient(gen_loss,
                                                generator.trainable_variables)
        discriminator_gradients = disc_tape.gradient(
            disc_loss, discriminator.trainable_variables)

        generator_optimizer.apply_gradients(
            zip(generator_gradients, generator.trainable_variables))
        discriminator_optimizer.apply_gradients(
            zip(discriminator_gradients, discriminator.trainable_variables))

        return gen_loss, disc_loss, gen_output

    # ------------------------------
    #        TRAINING LOOP
    # ------------------------------
    for epoch in range(epochs):

        start = time.time()

        # ------------------------------
        #  Extract the Training Dataset
        #    from the TFRecords file
        # ------------------------------
        training_dataset = get_dataset(pathdataset, batchsize)

        # ------------------------------
        # Loop over the training batches
        # ------------------------------
        for image_features in training_dataset:

            # Loop over all images in the batch
            for record_idx in range(batchsize):

                # ------------------------------
                #    Extract the individual
                #   features and  reconstruct
                #    the input images to feed
                #      the Neural Networks
                # ------------------------------
                a_image, b_image, _ = decode_tfrecords_example(
                    image_features, record_idx)

                # ------------------------------
                #   Perform Data Augmentation
                #     with the input images
                # ------------------------------
                a_image, b_image = transform_image(a_image, b_image)

                # ------------------------------
                #  Perform one TRAININING STEP
                # ------------------------------
                gen_loss, disc_loss, gen_output = train_step(
                    input_image=b_image, target=a_image)

        # ------------------------------
        #  Write tensorboard summaries
        #   at the end of the epoch
        # ------------------------------
        with summary_writer.as_default():
            tf.summary.scalar('Generator loss', gen_loss, step=epoch)
            tf.summary.scalar('Discriminator loss', gen_loss, step=epoch)
            tf.summary.image('input_image',
                             b_image,
                             max_outputs=epoch,
                             step=epoch)
            tf.summary.image('target_image',
                             a_image,
                             max_outputs=epoch,
                             step=epoch)
            tf.summary.image('generated_image',
                             gen_output,
                             max_outputs=epoch,
                             step=epoch)

        # ------------------------------
        # Saving (checkpoint) the model
        #        every 10 epochs
        # ------------------------------
        if (epoch + 1) % 10 == 0:
            manager.save()

        # ------------------------------
        #      Display progression
        # ------------------------------
        print('Epoch {}/{} ( {:.1f} sec)  gen_loss={:.6f}  disc_loss={:.6f}'.
              format(epoch + 1, epochs,
                     time.time() - start, gen_loss, disc_loss))

    print("End of training")

    # ------------------------------
    #     Write the Output of the
    #   Kubeflow Pipeline Component
    # ------------------------------
    try:
        # This works only inside Docker containers
        with open('/output.txt', 'w') as f:
            f.write(pathmodel)

    except PermissionError:
        pass

    return pathmodel
Ejemplo n.º 15
0
# load the model 
cnn_classifier = keras.models.load_model('cnn_models/west_train_model_dropout.h5')

#preparing data for predictions

size = (64, 64)
X_eval = list()
y_eval = list()

# crater part
files = os.listdir('./crater_data/test_set/crater')
files.sort()

for i in range(0, len(files) - 1):
    X_eval.append(transform_image('./crater_data/test_set/crater/' + files[i + 1], size))
    y_eval.append(1)

# non-crater part
files = os.listdir('./crater_data/test_set/non-crater')
files.sort()

for i in range(0, len(files) - 1):
    X_eval.append(transform_image('./crater_data/test_set/non-crater/' + files[i + 1], size))
    y_eval.append(0)

# stacking the arrays   
X_eval = np.vstack(X_eval)

cnn_pred = cnn_classifier.predict_classes(X_eval, batch_size = 32)
Ejemplo n.º 16
0
def run_inference():
    now = datetime.now()
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)

    for x in range(4):

        rep = session.next()
        try:
            if (rep["class"] == "TPV"):
                print(str(rep.lat) + "," + str(rep.lon))
                locate = ("Timestamp " + dt_string + "," + " Latitude: " +
                          str(rep.lat) + "," + "longitudes: " + str(rep.lon))

        except Exception as e:
            print("Got exception " + str(e))
    print(time)
    #os.system('fswebcam -r 1024x768 --no-banner --scale 224x224 output.jpg -S 7 --save /home/pi/Photos/std.jpg') # uses Fswebcam to take picture
    image = Image.open('output.jpg')
    #data = np.array(image,dtype='float64')
    #data=data1.reshape((1,data1.shape[2],data1.shape[0],data1.shape[1]))

    #np.save( 'flamingo.npy', data)

    image_data = utils.transform_image(image)
    #print(image_data)
    flattened_data = image_data.astype(np.float32).flatten()
    #np.save( 'puppi.npy',flattened_data)
    #print("Start Prinring Flattern")
    #print(flattened_data)
    #run_inference(image_data)
    #time.sleep(15) # this line creates a 15 second delay before repeating the loop

    model_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                              '../model-rasp3b')
    batch_size = 1
    channels = 3
    height = width = 224
    input_shape = {'input0': [batch_size, channels, height, width]}
    classes = 1000
    output_shape = [batch_size, classes]
    device = 'cpu'
    model = DLRModel(model_path, input_shape, output_shape, device)

    synset_path = os.path.join(model_path, 'imagenet1000_clsidx_to_labels.txt')
    with open(synset_path, 'r') as f:
        synset = eval(f.read())

    #image = np.load(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dog.npy')).astype(np.float32)
    #input_data = {'data': image_data}

    # Predict
    out = model.run({'input0': flattened_data}).squeeze()
    top1 = np.argmax(out)
    prob = np.max(out)

    # Using for loop
    for i in list:
        # How to use find()
        if (synset[top1].find(i) != -1):
            print("Contains given substring ")
            GPIO.output(12, GPIO.HIGH)  # Turn on
            sleep(10)  # Sleep for 1 second
            GPIO.output(12, GPIO.LOW)  # Turn off
        #sleep(10)

        #else:
        #print ("Doesn't contains given substring")
        #print(i)
    print("Class: %s, probability: %f" % (synset[top1], prob))
    #while True: # Run forever
    #GPIO.output(8, GPIO.HIGH) # Turn on
    #sleep(10) # Sleep for 1 second
    #GPIO.output(8, GPIO.LOW) # Turn off
    #sleep(10)

    #for rep in range(4):
    #t1 = current_milli_time()
    #out = model.run({'input0' : flattened_data}).squeeze()
    #t2 = current_milli_time()

    #logging.debug('done m.run(), time (ms): {}'.format(t2 - t1))

    #top1 = np.argmax(out)

    print(locate)
    logging.debug('Inference result: {}, {}'.format(locate, synset[top1]))
Ejemplo n.º 17
0
def predict_from_image(filename):
    image = Image.open(filename)
    image_data = utils.transform_image(image)

    #send_mqtt_message("Start predicting...")
    predict(image_data)
Ejemplo n.º 18
0
cnn_classifier = keras.models.load_model(
    'cnn_models/west_train_model_dropout.h5')

#preparing data for predictions

size = (64, 64)
X_eval = list()
y_eval = list()

# crater part
files = os.listdir('./crater_data/challenging_test_set/crater')
files.sort()

for i in range(0, len(files) - 1):
    X_eval.append(
        transform_image(
            './crater_data/challenging_test_set/crater/' + files[i + 1], size))
    y_eval.append(1)

# non-crater part
files = os.listdir('./crater_data/challenging_test_set/non-crater')
files.sort()

for i in range(0, len(files) - 1):
    X_eval.append(
        transform_image(
            './crater_data/challenging_test_set/non-crater/' + files[i + 1],
            size))
    y_eval.append(0)

# stacking the arrays
X_eval = np.vstack(X_eval)
def main():
    parser = argparse.ArgumentParser(
        description='Parser for Predicting Face Attribute')
    parser.add_argument('--model_path',
                        type=str,
                        required=True,
                        help='Path to model with .h5 extensions')
    parser.add_argument('--image',
                        type=str,
                        required=True,
                        help='Image filename to be predicted')
    parser.add_argument(
        '--preprocessing',
        type=str,
        default='resnet50',
        help=
        'Preprocessing method to be applied for the image, vgg16, inception_v3, or resnet50'
    )
    parser.add_argument('--img_size',
                        type=int,
                        default=224,
                        help='Image filename to be predicted')
    parser.add_argument('--save_image',
                        type=bool,
                        default=True,
                        help='Image filename to be predicted')

    args = parser.parse_args()

    model_path = args.model_path
    preprocessing = args.preprocessing
    filename = args.image
    size = (args.img_size, args.img_size)

    results = None

    # Load model
    model = load_pretrained(model_path)

    # Load image
    img = np.array(load_img(filename))

    # Detect faces using MTCNN face detector
    faces, bboxes = detect_face(img)

    if faces is not None:
        # Preprocess the image and expand its dimension
        trans_imgs = [
            transform_image(face, size, preprocessing) for face in faces
        ]

        # Predict face attributes
        preds = model.predict(np.array(trans_imgs))

        # Convert the output of predict and combine with its corresponding bbox
        results = convert_attribute(bboxes, preds)

        # Save the output image
        if args.save_image:
            img = draw_outputs(img, results)
            cv2.imwrite('out_{}'.format(filename), img)
            print('Output image saved at out_{}'.format(filename))

    print(results)
Ejemplo n.º 20
0
def get_screen(env):
    screen = env.render(mode='rgb_array')
    return utils.transform_image(screen)
Ejemplo n.º 21
0
    def train(self,
              epochs=50,
              batch_size=128,
              test_size=0.2,
              verbose=1,
              transform=1):
        #Si se quiere cargar el modelo lenet5 u otro, silenciar estas 3 lineas,
        #sino dara un error de compatibilidades de arquitectura.
        if os.path.exists(MODEL_NAME):
            self.load()
            return -1

        train_X, test_X, train_Y, test_Y = train_test_split(
            self.X, self.y, test_size=test_size)

        if verbose:
            print('Training data shape : ', train_X.shape, train_Y.shape)
            print('Testing data shape : ', test_X.shape, test_Y.shape)

        train_X = train_X.astype('float32')
        test_X = test_X.astype('float32')

        # Change the labels from categorical to one-hot encoding
        train_Y_one_hot = to_categorical(train_Y)

        if verbose:
            # Display the change for category label using one-hot encoding
            print('Original label:', train_Y[0])
            print('After conversion to one-hot:', train_Y_one_hot[0])

        train_X, valid_X, train_label, valid_label = train_test_split(
            train_X, train_Y_one_hot, test_size=test_size, random_state=13)

        # print(train_X.shape)

        if verbose:
            print(train_X.shape, valid_X.shape, train_label.shape,
                  valid_label.shape)

        # Data Augmentation
        if transform == 1:

            images_transform = []
            labels_transform = []

            for index, img in enumerate(train_X):

                aux_list = transform_image(img)

                images_transform += aux_list
                labels_transform += [train_label[index] for _ in aux_list]

            train_X = np.array(images_transform)
            train_label = np.array(labels_transform)

        self.history = self.model.fit(train_X,
                                      train_label,
                                      batch_size=batch_size,
                                      epochs=epochs,
                                      verbose=verbose,
                                      validation_data=(valid_X, valid_label))

        # guardamos la red, para reutilizarla en el futuro, sin tener que volver a entrenar
        self.model.save(MODEL_NAME)
    # for the other images (left and right).
    indexes = np.random.randint(0, len(images['center']), 64)

    for i in indexes:
        # random choice of center left or right
        camera_pos = np.random.choice(['center', 'left', 'right'])
        img = images[camera_pos][i]
        ang = angles[i]

        # adjust offet according to position
        if camera_pos == 'left':
            ang -= OFFSET
        elif camera_pos == 'right':
            ang += OFFSET
        # Execute transformations (rotation,shear,resize,brightness)
        img, ang = utils.transform_image(img, ang, input_size=input_size)
        # addition to global container
        X_train.append(img)
        y_train.append(ang)

# reserve a small portion for test the trained model. (0.01%)
X_train, X_test, y_train, y_test = train_test_split(np.array(X_train),
                                                    np.array(y_train),
                                                    test_size=0.001,
                                                    random_state=22)

########################################################
#               Model Architecture                     #
########################################################

# Definition of constants