Exemplo n.º 1
0
def predict_mask(test_img, crop_num = 5000, model_path = './scadec_Hydra/maskCNN/CNN_mask-2000.meta'):
    cropSize = (28,28)
    if len(np.shape(test_img)) == 3:
        test_img = test_img[:,:,0]
    
    test_img_patches, boxes = random_crop(test_img, crop_size = cropSize, crop_num = crop_num,
                      save = False, saveMode='npy')        
    test_img_patches = np.reshape(test_img_patches,list(np.shape(test_img_patches))+[1])
    test_img_patches_grad = np.reshape(get_gradients(test_img_patches[:,:,:,0]),(len(boxes), 28, 28, 1))
    test_data = np.zeros((len(boxes), 28,28,2))
            
    for idx in range(len(boxes)):
        test_data[idx, :, :, :] = np.concatenate([test_img_patches[idx,:,:], test_img_patches_grad[idx,:,:]], axis = 2)
    
    with tf.Session() as sess:
        new_saver = tf.train.import_meta_graph(model_path)
        new_saver.restore(sess, '.'.join(model_path.split('.')[:-1]))
        
        graph = tf.get_default_graph()
        xs = graph.get_operation_by_name('xs').outputs[0]
        pred = graph.get_operation_by_name('pred').outputs[0]            
        y_pre_raw = sess.run(pred, feed_dict={xs:test_data})
    
    # print(y_pre_raw[:50,0])
    y_pre = y_pre_raw[:,0]>0.9
    mask = np.zeros(np.shape(test_img))
    for idx, box in enumerate(boxes):
        if y_pre[idx] == True:
            mask[box[0]:box[1],box[2]:box[3]] += 0.01
    
    return mask
Exemplo n.º 2
0
def predict_masks_old2(test_imgs, crop_num = 5000, model_path = './scadec_Hydra/maskCNN/CNN_mask-2000.meta'):
    print('Computing masks...')
    startTime = time.time()

    # 1. Get croped patches    
    img_n, img_h, img_w, img_c = np.shape(test_imgs)
    masks = np.zeros(np.shape(test_imgs))
    
    cropSize = (28,28)    
    

    with tf.Session() as sess:
        # Get feed data(img+gradient)
        new_saver = tf.train.import_meta_graph(model_path)
        new_saver.restore(sess, '.'.join(model_path.split('.')[:-1]))
        
        graph = tf.get_default_graph()
        xs = graph.get_operation_by_name('xs').outputs[0]
        pred = graph.get_operation_by_name('pred').outputs[0]

        # Get patches
        print('Getting patches...')
        data = []
        for imgIdx in range(img_n):
            img = np.squeeze(test_imgs[imgIdx])
            img_patches, boxes = random_crop(img, crop_size = cropSize, crop_num = crop_num,
                        save = False, saveMode='npy')        
            img_patches = np.reshape(img_patches,list(np.shape(img_patches))+[1])
            img_patches_grad = np.reshape(get_gradients(img_patches[:,:,:,0]),(len(boxes), 28, 28, 1))
            
            datum = np.zeros((len(boxes), 28,28,2))
            for idx in range(len(boxes)):
                datum[idx, :, :, :] = np.concatenate([img_patches[idx,:,:], img_patches_grad[idx,:,:]], axis = 2)                
            data.append(datum)
        
        # Get Predictions
        print('Predicting...')
        y_pre_raws = []
        for imgIdx in range(img_n):
            y_pre_raws.append(sess.run(pred, feed_dict={xs:data[imgIdx]}))
        
        # Get masks
        # y_pres = []
        print('Computing masks...')
        for imgIdx in range(img_n):
            y_pre = y_pre_raws[imgIdx][:,0]>0.9
            mask = np.zeros(np.shape(img))
            for idx, box in enumerate(boxes):
                if y_pre[idx] == True:
                    mask[box[0]:box[1],box[2]:box[3]] += 0.01
            masks[imgIdx] = np.reshape(mask, [img_h, img_w, img_c])
        endTime = time.time()
        print('Finished computing masks within {%.2f} mins for {} images!'.format((endTime - startTime)/60, img_n))
    return masks
Exemplo n.º 3
0
                            np.arange(99, 245, 5), :, :, :]

    return data, vdata, truths, vtruths


#%% 1. Getting patches
crop_num = 5000
cropSize = (28, 28)

imgs = get_data(para_dict_use_train, DEBUG_MODE=False)

img_n, img_h, img_w, img_c = np.shape(imgs)
data = np.zeros([img_n * crop_num, img_h, img_w, img_c])
for imgIdx in range(img_n):
    img = np.squeeze(imgs[imgIdx])
    img_patches, boxes = random_crop(img,
                                     crop_size=cropSize,
                                     crop_num=crop_num,
                                     save=False,
                                     saveMode='npy')
    img_patches = np.reshape(img_patches, list(np.shape(img_patches)) + [1])
    img_patches_grad = np.reshape(get_gradients(img_patches[:, :, :, 0]),
                                  (len(boxes), 28, 28, 1))

    datum = np.zeros((len(boxes), 28, 28, 2))
    for idx in range(len(boxes)):
        datum[idx, :, :, :] = np.concatenate(
            [img_patches[idx, :, :], img_patches_grad[idx, :, :]], axis=2)
    data[img_n]

model_path = './scadec_Hydra/maskCNN/CNN_mask-2000.meta'
Exemplo n.º 4
0
def predict_masks(test_imgs,
                  crop_num=5000,
                  model_path='./scadec_Hydra/maskCNN/CNN_mask-2000.meta',
                  save_path='..data/masks/',
                  saveName='patches',
                  loadName='patches',
                  savePatches=False,
                  loadPatches=False,
                  saveYPre=False,
                  loadYPre=False,
                  saveMasks=False,
                  loadMaskes=False):  #, savePath = None):
    # import os
    # print(os.getcwd())
    print('Computing masks...')
    startTime = time.time()

    # 1. Get croped patches
    img_n, img_h, img_w, img_c = np.shape(test_imgs)
    # print(np.shape(masks))

    cropSize = (28, 28)
    savefile_img_num = 2000

    with tf.Session() as sess:
        # Get feed data(img+gradient)
        new_saver = tf.train.import_meta_graph(model_path)
        new_saver.restore(sess, '.'.join(model_path.split('.')[:-1]))

        graph = tf.get_default_graph()
        xs = graph.get_operation_by_name('xs').outputs[0]
        pred = graph.get_operation_by_name('pred').outputs[0]

        # Get patches
        print('Getting patches...')
        if loadPatches:
            patches, boxeses = np.load(save_path +
                                       '{}_{}_{}_patches_boxeses.npy'.format(
                                           loadName, cropSize[0], cropSize[1]))
            # with open('./scadec_Hydra/maskCNN/pre_computed/{}.pkl'.format(loadName), 'rb') as f:  # Python 3: open(..., 'wb')
            #     data = pickle.load(f)
        elif (not loadYPre) and (not loadMaskes):
            patches = np.zeros([img_n * crop_num, cropSize[0], cropSize[1], 2])
            boxeses = []
            # for preTraIdx in tqdm(range(500), ncols=75):
            for imgIdx in tqdm(range(img_n), ncols=75):
                img = np.squeeze(test_imgs[imgIdx])
                img_patches, boxes = random_crop(img,
                                                 crop_size=cropSize,
                                                 crop_num=crop_num,
                                                 save=False,
                                                 saveMode='npy')
                img_patches = np.reshape(img_patches,
                                         list(np.shape(img_patches)) + [1])
                img_patches_grad = np.reshape(
                    get_gradients(img_patches[:, :, :, 0]),
                    (len(boxes), 28, 28, 1))

                datum = np.zeros((len(boxes), 28, 28, 2))
                for idx in range(len(boxes)):
                    datum[idx, :, :, :] = np.concatenate(
                        [img_patches[idx, :, :], img_patches_grad[idx, :, :]],
                        axis=2)
                # data.append(datum)
                patches[imgIdx * crop_num:(imgIdx + 1) *
                        crop_num, :, :, :] = datum
                boxeses.append(boxes)

        if savePatches:
            print('Saving...')
            # np.save('./scadec_Hydra/maskCNN/pre_computed/{}_{}_{}_patches_boxeses'.format(saveName, cropSize[0], cropSize[1]), [patches, boxeses])
            np.save(
                save_path +
                '{}_{}_{}_patches'.format(saveName, cropSize[0], cropSize[1]),
                patches)

        # Get Predictions
        print('Predicting...')
        if loadYPre:
            y_pre_raws = np.load(save_path + '{}_{}_{}_ypre_raws.npy'.format(
                saveName, cropSize[0], cropSize[1]))
            boxeses = np.load(save_path + '{}_{}_{}_boxeses.npy'.format(
                saveName, cropSize[0], cropSize[1]))
        elif not loadMaskes:
            y_pre_raws = []
            for imgIdx in tqdm(range(img_n), ncols=75):
                y_pre_raws.append(
                    sess.run(pred,
                             feed_dict={
                                 xs:
                                 patches[imgIdx * crop_num:(imgIdx + 1) *
                                         crop_num]
                             }))

            # a = 1
        if saveYPre:
            np.save(
                save_path + '{}_{}_{}_ypre_raws.npy'.format(
                    saveName, cropSize[0], cropSize[1]), y_pre_raws)
            np.save(
                save_path + '{}_{}_{}_boxeses.npy'.format(
                    saveName, cropSize[0], cropSize[1]), boxeses)

        # Get masks
        # y_pres = []
        print('Computing masks...')
        fMean = np.ones((5, 5)) / 25
        masks = np.zeros(np.shape(test_imgs))
        for imgIdx in tqdm(range(img_n), ncols=75):
            # y_pre = y_pre_raws[imgIdx][:,0]>0.9
            boxes = boxeses[imgIdx]
            mask = np.zeros(np.shape(test_imgs[0, :, :, 0]))
            for idx, box in enumerate(boxes):
                mask[box[0]:box[1],
                     box[2]:box[3]] += 0.01 * y_pre_raws[imgIdx][idx, 0]
                # if y_pre[idx] == True:
                #     mask[box[0]:box[1],box[2]:box[3]] += 0.01
            mask = convolve2d(mask, fMean, mode='same')
            masks[imgIdx] = np.reshape(mask, [img_h, img_w, img_c])

        if saveMasks:
            np.save(
                save_path +
                '{}_{}_{}_masks'.format(saveName, cropSize[0], cropSize[1]),
                masks)
        endTime = time.time()
        print('Finished computing masks for {} within {} mins for {} images!'.
              format(saveName, (endTime - startTime) / 60, img_n))
    return masks