def get_images_with_filenames(filenames): #### read a mini batch data inputs, targets = load_images(filenames, deterministic=True) new_inputs = [] new_targets = [] for image, target in zip(inputs, targets): ims, trs = testtime_augmentation(image[0], target) # Take color channel of image new_inputs += ims new_targets += trs new_filenames = [] for fname in filenames: for i in range(int(len(new_inputs) / len(filenames))): new_filenames.append(fname) return np.array(new_inputs, dtype=np.float32), np.array(new_targets, dtype=np.int32), new_filenames
def get_images_with_filenames(filenames): inputs, targets = load_images(filenames, deterministic=True) new_inputs = [] new_targets = [] for image, target in zip(inputs, targets): ims, trs = augment.testtime_augmentation(image[0], target) #Take color channel of image new_inputs += ims new_targets+=trs new_filenames = [] for fname in filenames: for i in range(int(len(new_inputs)/len(filenames))): new_filenames.append(fname) return np.array(new_inputs,dtype=np.float32),np.array(new_targets,dtype=np.int32), new_filenames
def main(): model_path = sys.argv[1] net_file = sys.argv[2] ### load all predicting filenames filenames = glob.glob(P.FILENAMES_PREDICTION) batch_size = P.BATCH_SIZE_PREDICTION ### get augmentation number test_im = np.zeros((64, 64)) n_testtime_augmentation = len(testtime_augmentation(test_im, 0)[0]) #### get the parallel data generator gen = ParallelBatchIterator(get_images_with_filenames, filenames, ordered=True, batch_size=batch_size // (3 * n_testtime_augmentation), multiprocess=False, n_producers=12) ### get wide resnet caffe model caffe_net = caffe.Net(net_file, model_path, caffe.TEST) ### do forward pass all_probabilities = [] all_filenames = [] print('begin predicting...') for i, batch in tqdm(enumerate(gen)): data, label, fnames = batch ### pass data and label to caff net, please set the batch size to 12(atomic batchsize) for both prediction batch size and train batch size if data.shape[0] == batch_size: caffe_net.blobs['data'].data[...] = data.astype(np.float32, copy=False) caffe_net.blobs['label'].data[...] = label.astype(np.float32, copy=False) caffe_net.forward() softmax_out = caffe_net.blobs['prob'].data.copy() all_probabilities += list(softmax_out[:, 1].tolist()) all_filenames += list(fnames) # print("one batch done") else: break ### for all filenames get the probalilities(3 * n_testtime_augmentation) d = {f: [] for f in filenames} for probability, f in zip(all_probabilities, all_filenames): d[f].append(probability) ### the code will not run unless the batch size is not the atomic batch size(3 * n_testtime_augmentation) for key in d.keys(): if len(d[key]) == 0: d.pop(key) print('end predicting') print('write to csv') cur_dir = os.path.dirname(os.path.abspath(__file__)) candidates = pd.read_csv( os.path.join(cur_dir, './../../../data/finalizedcandidates_unet_01.csv')) data = [] ### get the mean prob for each filename and get the row number of the candidate for x in d.iteritems(): fname, probabilities = x prob = np.mean(probabilities) candidates_row = int(os.path.split(fname)[1].replace('.pkl.gz', '')) - 2 print candidates_row data.append( list(candidates.iloc[candidates_row].values)[:-1] + [str(prob)]) ### write the prob to a .csv submission = pd.DataFrame( columns=['seriesuid', 'coordX', 'coordY', 'coordZ', 'probability'], data=data) submission_path = os.path.join(cur_dir, './../../../data/submission_subset01.csv') submission.to_csv( submission_path, columns=['seriesuid', 'coordX', 'coordY', 'coordZ', 'probability']) print('finished!')
print "Loading saved model", model_save_file with np.load(model_save_file) as f: param_values = [f['arr_%d' % i] for i in range(len(f.files))] lasagne.layers.set_all_param_values(network, param_values) print "Defining updates.." train_fn, val_fn, l_r = resnet.define_updates(network, input_var, target_var) in_pattern = '/media/diskB/guido/luna16/candidates_v2_0.5mm_96x96_xy_xz_yz/subset[{}]/*/*.pkl.gz'.format(subsets) filenames = glob(in_pattern) batch_size = 1200 multiprocess = True test_im = np.zeros((64,64)) n_testtime_augmentation = len(augment.testtime_augmentation(test_im, 0)[0]) def get_images_with_filenames(filenames): inputs, targets = load_images(filenames, deterministic=True) new_inputs = [] new_targets = [] for image, target in zip(inputs, targets): ims, trs = augment.testtime_augmentation(image[0], target) #Take color channel of image new_inputs += ims new_targets+=trs new_filenames = [] for fname in filenames: for i in range(int(len(new_inputs)/len(filenames))):
lasagne.layers.set_all_param_values(network, param_values) print "Defining updates.." train_fn, val_fn, l_r = resnet.define_updates(network, input_var, target_var) #in_pattern = '../../data/cadV2_0.5mm_64x64_xy_xz_yz/subset[{}]/*/*.pkl.gz'.format(subsets) in_pattern = '/scratch-shared/vdgugten/data/cadOWN_0.5mm_96x96_xy_xz_yz/subset[{}]/*/*.pkl.gz'.format( subsets) filenames = glob(in_pattern) batch_size = 1200 multiprocess = True test_im = np.zeros((64, 64)) n_testtime_augmentation = len(augment.testtime_augmentation(test_im, 0)[0]) def get_images_with_filenames(filenames): inputs, targets = load_images(filenames, deterministic=True) new_inputs = [] new_targets = [] for image, target in zip(inputs, targets): ims, trs = augment.testtime_augmentation( image[0], target) #Take color channel of image new_inputs += ims new_targets += trs new_filenames = [] for fname in filenames:
from skimage import util import matplotlib.pyplot as plt from matplotlib.pyplot import imread import scipy import numpy as np if __name__ == "__main__": image = imread("example.jpg") # print image image = image[:, :, 1] # plt.subplot(211) # image=np.array(image) # print image.shape # plt.imshow(list(image)) # image2 = augment.augment([image]) # plt.subplot(212) # plt.imshow(image2[0]) # print image2[0].shape # plt.show() images = augment.testtime_augmentation(np.array(image)) print len(images) plt.subplot(221) plt.imshow(images[0]) plt.subplot(222) plt.imshow(images[1]) plt.subplot(223) plt.imshow(images[2]) plt.subplot(224) plt.imshow(images[3]) plt.show()
import matplotlib.pyplot as plt from matplotlib.pyplot import imread import scipy import numpy as np if __name__ == "__main__": image = imread("example.jpg") #print image image = image[:,:,1] # plt.subplot(211) # image=np.array(image) # print image.shape # plt.imshow(list(image)) # image2 = augment.augment([image]) # plt.subplot(212) # plt.imshow(image2[0]) # print image2[0].shape # plt.show() images = augment.testtime_augmentation(np.array(image)) print len(images) plt.subplot(221) plt.imshow(images[0]) plt.subplot(222) plt.imshow(images[1]) plt.subplot(223) plt.imshow(images[2]) plt.subplot(224) plt.imshow(images[3]) plt.show()