コード例 #1
0
import numpy
import cv2
import torch
import time
from models.net_1.model import Model

model_input_width = 512  #96
model_input_height = 512  #96

model = Model((1, model_input_height, model_input_width), 2)
model.load("models/net_1/")

cap = cv2.VideoCapture("/Users/michal/Movies/line_path_video.mp4")
input_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
input_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
'''
#cap = cv2.VideoCapture(0)
input_width        = 640
input_height       = 480
cap.set(cv2.CAP_PROP_FRAME_WIDTH,input_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,input_height)
'''


def get_prediction(frame):
    frame_grayscale = numpy.array(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY))
    frame_resized = cv2.resize(frame_grayscale,
                               (model_input_width, model_input_height),
                               interpolation=cv2.INTER_AREA)

    frame_normalised = numpy.clip(frame_resized / 255.0, 0, 1)
コード例 #2
0
ファイル: train.py プロジェクト: michalnand/line_follower_rl
#root_path = "/home/michal/dataset/background/"

dataset = Dataset(96, 96, 10000, 1000, root_path)

 

testing_loss_sum_best = None

nets_to_try = 1

for net in range(nets_to_try):

    epoch_count     = 20
    learning_rates  = [0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.00001, 0.00001]

    model = Model(dataset.training.input_shape, dataset.training.output_shape[0])

    for epoch in range(epoch_count):
        
        batch_size  = 32 
        batch_count = (dataset.training.get_count()+batch_size) // batch_size

        learning_rate = learning_rates[epoch%len(learning_rates)]
        
        optimizer  = torch.optim.Adam(model.parameters(), lr= learning_rate, weight_decay=10**-6)  

        training_loss_sum = 0.0
        for batch_id in range(batch_count):
            training_x, training_y = dataset.training.get_batch(batch_size)

            training_x = training_x.to(model.device)
コード例 #3
0
    input_rgb[1] = 0.5 * input_rgb[1] + 0.5 * output_upscaled

    result = input_rgb
    result = numpy.rollaxis(result, 0, 2)
    result = numpy.rollaxis(result, 2, 1)

    result = (result * 255).astype(dtype=numpy.uint8)
    image = Image.fromarray(result)
    image.show()


size = 96
dataset = Dataset(size, size, 100, 100, root_path)

model = Model(dataset.training.input_shape, dataset.training.output_shape[0])

model.load("models/net_1/")

batch_size = 10

input, target = dataset.testing.get_batch()

prediction = model.forward(input)

for i in range(batch_size):
    input_np = input[i][0].detach().numpy()

    target_np = numpy.clip(target[i][0].detach().numpy(), 0, 1)
    prediction_np = numpy.clip(prediction[i][0].detach().numpy(), 0, 1)
コード例 #4
0
from models.net_1.model import Model
import torch
import metrics

dataset = Dataset("dataset_config_training.json",
                  "dataset_config_testing.json")

testing_loss_sum_best = None

epoch_count = 100
learning_rates = [
    0.001, 0.001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.00001,
    0.00001
]

model = Model(dataset.training.input_shape, dataset.training.output_shape[0])

for epoch in range(epoch_count):

    batch_size = 32
    batch_count = (dataset.training.get_count() + batch_size) // batch_size

    learning_rate = learning_rates[epoch % len(learning_rates)]

    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=learning_rate,
                                 weight_decay=10**-6)

    training_loss_sum = 0.0
    for batch_id in range(batch_count):
        training_x, training_y = dataset.training.get_batch(batch_size)
コード例 #5
0
        macs = 4 * size

        print("export_ReLU :")
        print("IO shape        ", output_shape)
        print("macs            ", macs)
        print("\n\n")

        return code, output_shape, size, macs

    def quantize(self, weights, bias):
        tmp = numpy.concatenate([weights.flatten(), bias.flatten()])
        scale = numpy.std(tmp) * 2

        result_weights = numpy.clip((weights * 128) / scale, -127,
                                    127).astype(numpy.int8)
        result_bias = numpy.clip((bias * 128) / scale, -127,
                                 127).astype(numpy.int8)

        return result_weights, result_bias, scale


model_input_height = 192
model_input_width = 192
model_output_channels = 1

model = Model((1, model_input_height, model_input_width),
              model_output_channels)
model.load("models/net_1/")

export = ExportNetwork(model, model_input_height, model_input_width, 1)