コード例 #1
0
    def __getitem__(self, index):
        label = []  # list of annotation id  for one image
        ann = []  # top 3  annotation from list of annotations for one image
        padding = 20
        print('INDEXXXX', index)
        img = self.imgMap[index]
        anns = self.labelIds[index]
        print(img)
        for key in img:
            x = io.imread(img.get(key))
        x = Image.fromarray(x.astype('uint8'), 'RGB')
        x = preprocess.preprocess_img(x)

        annotations = []
        for ann in anns:
            tokens = nltk.tokenize.word_tokenize(str(ann).lower())
            annotation = []
            annotation.append(self.vocab('<start>'))
            annotation.extend([self.vocab(token) for token in tokens])
            annotation.append(self.vocab('<end>'))
            # if len(annotation) < padding:
            #     for i in range(padding - len(annotation)):
            #         annotation.append(0)

            annotations.append(torch.Tensor(annotation))
        print('Annotations', annotations)

        # temp = TemporaryFile()
        # np.save(temp, x)
        # x = torch.load(temp)
        return x, annotations
コード例 #2
0
def telemetry(sid, data):
    if data:
        # The current steering angle of the car
        steering_angle = data["steering_angle"]
        # The current throttle of the car
        throttle = data["throttle"]
        # The current speed of the car
        speed = data["speed"]
        # The current image from the center camera of the car
        imgString = data["image"]
        image = Image.open(BytesIO(base64.b64decode(imgString)))
        image_array = preprocess_img(np.asarray(image))

        image_array = np.reshape(image_array, (3, 66, 200))
        steering_angle = float(model.predict(image_array[None, :, :, :], batch_size=1))
        speed = float(speed)


        throttle = 0.12 # controller.update(float(speed))

        print('angle:', steering_angle, 'throttle:', throttle)
        send_control(steering_angle, throttle)

        # save frame
        if args.image_folder != '':
            timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3]
            image_filename = os.path.join(args.image_folder, timestamp)
            image.save('{}.jpg'.format(image_filename))
    else:
        # NOTE: DON'T EDIT THIS.
        sio.emit('manual', data={}, skip_sid=True)
コード例 #3
0
 def getimage(self, id):
     img = self.coco.loadImgs(id)
     img = self.coco.loadImgs(self.imgIds[np.random.randint(
         0, len(self.imgIds))])[0]
     I = io.imread(img['coco_url'])
     im = Image.fromarray(I)
     X = preprocess.preprocess_img(im)
     print('type of I', type(I), '\n Shape of X :', X.shape)
     return X
def test_model(model):
    test = pd.read_csv('GTSRB/GT-final_test.csv', sep=';')

    # Load test dataset
    X_test = []
    y_test = []
    i = 0
    for file_name, class_id in zip(list(test['Filename']),
                                   list(test['ClassId'])):
        img_path = os.path.join('GTSRB/Final_Test/Images/', file_name)
        X_test.append(preprocess_img(io.imread(img_path)))
        y_test.append(class_id)

    X_test = np.array(X_test)
    y_test = np.array(y_test)

    # predict and evaluate
    y_pred = model.predict_classes(X_test)
    accuracy = float(np.sum(y_pred == y_test)) / float(np.size(y_pred))
    return accuracy
コード例 #5
0
def main():
    parser = build_parser()
    args = parser.parse_args()

    if not os.path.exists(args.outdir):
        os.makedirs(args.outdir)

    # Load image and mask
    img = np.array(Image.open(args.input).resize((256, 256)), dtype=np.float32)
    mask = np.array(Image.open(args.mask).convert('L').resize((256, 256)),
                    dtype=np.float32)

    if img is None or mask is None:
        raise Exception('Image {} or mask {} not found'.format(
            args.img, args.mask))

    img, mask = preprocess_img(img, mask)
    img_in = torch.FloatTensor(img.transpose(2, 0, 1)).unsqueeze(0)
    rnd_mat = torch.rand_like(img_in).to(device)
    rnd_mat = rnd_mat * 0.1
    if args.arch == 'resnet':
        raise Exception('Resnet not implemented currently.')

    elif args.arch == 'unet':
        net = MultiresUNet(num_input_channels=3,
                           num_output_channels=3,
                           nlevels=args.nlevels,
                           upsample_mode='nearest',
                           need_sigmoid=True,
                           need_bias=True,
                           pad='reflection')

    recon_loss = nn.MSELoss()
    level_losses = [nn.MSELoss() for i in range(args.nlevels)]
    recon_op = ReconstructPyramid(args.type, args.nlevels)

    x = get_pyramid(img, args.type, args.nlevels)
    x = [i.to(device) for i in x]
    mask_t = torch.FloatTensor(
        np.dstack([mask, mask, mask]).transpose(2, 0,
                                                1)).unsqueeze(0).to(device)
    masks_pyr = []
    tmp_mask = mask_t.clone()
    for i in range(args.nlevels):
        masks_pyr.append(tmp_mask)
        mask_pyr = F.interpolate(tmp_mask, scale_factor=0.5)
        tmp_mask = mask_pyr.clone()
    net = net.to(device)
    recon_op = recon_op.to(device)
    opt = torch.optim.Adam(net.parameters(), lr=args.lr)
    for epoch in range(args.epochs):
        net.train()
        gen_pyr = net(rnd_mat)
        recon_img = recon_op(gen_pyr)

        recon_loss_val = recon_loss(recon_img * mask_t, img_in * mask_t)
        level_loss_val = torch.tensor(0.0)
        for i in range(1, args.nlevels):
            #print(x[i].size(), gen_pyr[i].size())
            level_loss_val += level_losses[i](gen_pyr[i] * masks_pyr[i],
                                              x[i] * masks_pyr[i])
            #level_loss_val += level_losses[i](gen_pyr[i], x[i])
        #level_loss_vals = level_losses[i](gen_pyr[i]) for i in range(args.nlevels)]
        opt.zero_grad()
        total_loss = recon_loss_val  #+ level_loss_val
        total_loss.backward()
        opt.step()

        print(
            'Total loss:{:.05f}, recon loss:{:.05f}, levelwise loss: {:.05f}'.
            format(total_loss.item(), recon_loss_val.item(),
                   level_loss_val.item()))
        if epoch % args.save == 0:
            net.eval()
            with torch.no_grad():
                out_pyr = net(rnd_mat)
                #print(out_pyr[0].shape)
                output_img = out_pyr[0][0,
                                        ...].cpu().detach().numpy().transpose(
                                            1, 2, 0)
                fig = plt.figure()
                ax = fig.add_subplot(1, 1, 1)
                ax.tick_params(top=False,
                               left=False,
                               right=False,
                               bottom=False,
                               labelleft=False,
                               labelbottom=False)
                ax.imshow(output_img)
                plt.savefig(os.path.join(args.outdir, '{}.png'.format(epoch)),
                            bbox_inches='tight'),
                plt.close()
                np.save(os.path.join(args.outdir, '{}-img'.format(epoch)),
                        output_img)