def test_bin_spatial(path, dcspace='RGB'): cars, noncars = load_training_images(path) target_cspace = dcspace # read a car image and get feature ind = np.random.randint(0, len(cars)) car_name = cars[ind] car_image = load_image(car_name, dcspace) car_feature = bin_spatial(car_image, size=(32, 32)) # read a non car image and get feature ind = np.random.randint(0, len(noncars)) noncar_name = noncars[ind] noncar_image = load_image(noncar_name, dcspace) noncar_feature = bin_spatial(noncar_image, size=(32, 32)) # Plot features fig = plt.figure() plt.subplot(221) plt.imshow(color_convert_nocheck(car_image, dcspace, 'RGB')) plt.xlabel(car_name) plt.subplot(222) plt.plot(car_feature) plt.title('Spatially Binned Features') plt.suptitle(dcspace) plt.subplot(223) plt.imshow(color_convert_nocheck(noncar_image, dcspace, 'RGB')) plt.xlabel(noncar_name) plt.subplot(224) plt.plot(noncar_feature) plt.title('Spatially Binned Features') plt.suptitle(dcspace) fig.savefig("output_images/bin_spatial.jpg") plt.show() plt.close()
def test_hog(path): cars, noncars = load_training_images(path) # Generate a random index to look at a car image car_ind = np.random.randint(0, len(cars)) noncar_ind = np.random.randint(0, len(noncars)) # Read in the image car_image = load_image(cars[car_ind], 'RGB') car_gray = cv2.cvtColor(car_image, cv2.COLOR_RGB2GRAY) noncar_image = load_image(noncars[noncar_ind], 'RGB') noncar_gray = cv2.cvtColor(noncar_image, cv2.COLOR_RGB2GRAY) # Define HOG parameters orient = 9 pix_per_cell = 8 cell_per_block = 2 # Call our function with vis=True to see an image output features, car_hog_image = get_hog_features(car_gray, orient, pix_per_cell, cell_per_block, vis=True, feature_vec=False) features, noncar_hog_image = get_hog_features(noncar_gray, orient, pix_per_cell, cell_per_block, vis=True, feature_vec=False) fig = plt.figure(figsize=(10, 8)) plt.subplot(231) plt.imshow(car_image) plt.xlabel(cars[car_ind]) plt.title('Example Car Image') plt.subplot(232) plt.imshow(car_gray, cmap='gray') plt.title('Gray Car Image') plt.subplot(233) plt.imshow(car_hog_image, cmap='gray') plt.title('HOG Visualization') plt.subplot(234) plt.imshow(noncar_image) plt.xlabel(noncars[noncar_ind]) plt.title('Example Non-car Image') plt.subplot(235) plt.imshow(noncar_gray, cmap='gray') plt.title('Gray Non-car Image') plt.subplot(236) plt.imshow(noncar_hog_image, cmap='gray') plt.title('HOG Visualization') plt.suptitle('HOG') fig.tight_layout() fig.savefig("output_images/hog.jpg")
def classify(model_location, image_location, results_location, print_result=False, array=False): """Classify given image file and return detected sign name.""" # Load model from file. model = joblib.load(model_location) # Load and preprocess the image. if array is False: image = preprocess_single(load_image(image_location)) else: image = preprocess_single(image_location) # Predict road sign. prediction = model.predict(image) # Load available road signs names. with open(results_location) as file_name: results = file_name.readlines() results = [x.strip() for x in results] # Print road sign name. end = len(results) for i in range(end): if prediction[0] == i: result = results[i] label = i if print_result: print('\nLabel of predicted road sign: ', prediction[0]) print('Name of predicted road sign: ', result) return result, label
def scan_images_for_maxes(net, datadir, filelist, n_top): image_filenames, image_xmls, image_labels = load_file_list(filelist) print('Scanning %d files' % len(image_filenames)) print(' First file', os.path.join(datadir, image_filenames[0])) tracker = NetMaxTracker(n_top=n_top) for image_idx in xrange(len(image_filenames)): filename = image_filenames[image_idx] roiname = image_xmls[image_idx] image_class = image_labels[image_idx] do_print = (image_idx % 100 == 0) if do_print: print('%s Image %d/%d' % (datetime.now().ctime(), image_idx, len(image_filenames))) with WithTimer('Load image', quiet=not do_print): im = load_image(os.path.join(datadir, filename)) _, _, rois = parse_roi_xml(os.path.join(datadir, roiname)) rois = np.array(rois) with WithTimer('Predict ', quiet=not do_print): net.blobs['data'].data[...] = im net.blobs['rois'].reshape(1, *rois.shape) net.blobs['rois'].data[...] = rois net.forward() with WithTimer('Update ', quiet=not do_print): tracker.update(net, image_idx, image_class) print('done!') return tracker
def load_and_process_image_fixed(img_index, subset, ds_transforms, augmentation_transforms, target_sizes=None): if subset == 'train': img_id = load_data.train_ids[img_index] elif subset == 'test': img_id = load_data.test_ids[img_index] img = load_data.load_image(img_id, from_ram=True, subset=subset) img_a = augment_fixed_and_dscrop(img, ds_transforms, augmentation_transforms, target_sizes) return img_a
def load_and_process_image(img_index, ds_transforms, augmentation_params, target_sizes=None): # start_time = time.time() img_id = load_data.train_ids[img_index] img = load_data.load_image(img_id, from_ram=True) # load_time = (time.time() - start_time) * 1000 # start_time = time.time() img_a = perturb_and_dscrop(img, ds_transforms, augmentation_params, target_sizes) # augment_time = (time.time() - start_time) * 1000 # print "load: %.2f ms\taugment: %.2f ms" % (load_time, augment_time) return img_a
def load_and_process_image_brightness_norm(img_index, ds_transforms, augmentation_params, target_sizes=None): img_id = load_data.train_ids[img_index] img = load_data.load_image(img_id, from_ram=myLoadFrom_RAM) img = img / img.max() # normalise img_a = perturb_and_dscrop(img, ds_transforms, augmentation_params, target_sizes) return img_a
def load_and_process_image_fixed_pysexgen1_centering_rescaling(img_index, subset, ds_transforms, augmentation_transforms, target_sizes=None): if subset == 'train': img_id = load_data.train_ids[img_index] elif subset == 'test': img_id = load_data.test_ids[img_index] tf_center_rescale = build_pysexgen1_center_rescale_transform(img_index, subset) img = load_data.load_image(img_id, from_ram=True, subset=subset) img_a = augment_fixed_and_dscrop_with_prepro(img, ds_transforms, augmentation_transforms, target_sizes, prepro_transform=tf_center_rescale) return img_a
def load_and_process_image_pysexgen1_centering_rescaling(img_index, ds_transforms, augmentation_params, target_sizes=None): # start_time = time.time() img_id = load_data.train_ids[img_index] img = load_data.load_image(img_id, from_ram=True) # load_time = (time.time() - start_time) * 1000 # start_time = time.time() tf_center_rescale = build_pysexgen1_center_rescale_transform(img_index) img_a = perturb_and_dscrop_with_prepro(img, ds_transforms, augmentation_params, target_sizes, prepro_transform=tf_center_rescale) # augment_time = (time.time() - start_time) * 1000 # print "load: %.2f ms\taugment: %.2f ms" % (load_time, augment_time) return img_a
def load_and_process_image_fixed_brightness_norm(img_index, subset, ds_transforms, augmentation_transforms, target_sizes=None): if subset == 'train': img_id = load_data.train_ids[img_index] elif subset == 'test': img_id = load_data.test_ids[img_index] img = load_data.load_image(img_id, from_ram=myLoadFrom_RAM, subset=subset) img = img / img.max() # normalise img_a = augment_fixed_and_dscrop(img, ds_transforms, augmentation_transforms, target_sizes) return img_a
def extract_color_feature(fname, spatial_color, spatial_size, hist_color, hist_bins=32, hist_range=(0, 256)): ''' extract color features ''' image = load_image(fname) spatial_image = color_convert_nocheck(image, 'BGR', spatial_color) spatial_feature = bin_spatial(spatial_image, size=spatial_size) hist_image = color_convert_nocheck(image, 'BGR', hist_color) hist_feature = color_hist_feature(hist_image, nbins=hist_bins, bins_range=hist_range) return np.concatenate((spatial_feature, hist_feature))
def extract_hog_features(fnames, cspace='RGB', orient=9, pix_per_cell=8, cell_per_block=2, hog_channel='012'): # Create a list to append feature vectors to features = [] # Iterate through the list of images for fname in fnames: # apply color conversion if other than 'RGB' image = load_image(fname, cspace) # Call get_hog_features() with vis=False, feature_vec=True hog_features = [] if len(image.shape) == 3: channels = image.shape[2] else: channels = 1 for channel in range(channels): if channels == 1: hog_features.append( get_hog_features(image[:, :], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) elif str(channel) in hog_channel: hog_features.append( get_hog_features(image[:, :, channel], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) if len(hog_features) == 0: break hog_features = np.ravel(hog_features) features.append(hog_features) return features
def save_representations(net, datadir, filelist, layer, first_N=None): image_filenames, image_labels = load_file_list(filelist) if first_N is None: first_N = len(image_filenames) assert first_N <= len(image_filenames) image_indices = range(first_N) print('Scanning %d files' % len(image_indices)) assert len(image_indices) > 0 print(' First file', os.path.join(datadir, image_filenames[image_indices[0]])) indices = None rep = None for ii, image_idx in enumerate(image_indices): filename = image_filenames[image_idx] roiname = image_xmls[image_idx] image_class = image_labels[image_idx] do_print = (image_idx % 10 == 0) if do_print: print('%s Image %d/%d' % (datetime.now().ctime(), image_idx, len(image_indices))) with WithTimer('Load image & ROIs', quiet=not do_print): im = load_image(os.path.join(datadir, filename)) _, _, rois = parse_roi_xml(os.path.join(datadir, roiname)) rois = np.array(rois) with WithTimer('Predict ', quiet=not do_print): net.blobs['data'].data[...] = im net.blobs['rois'].reshape(1, *rois.shape) net.blobs['rois'].data[...] = rois net.forward() with WithTimer('Store ', quiet=not do_print): if rep is None: rep_shape = net.blobs[layer].data[0].shape # e.g. (256,13,13) rep = np.zeros((len(image_indices), ) + rep_shape) # e.g. (1000,256,13,13) indices = [0] * len(image_indices) indices[ii] = image_idx rep[ii] = net.blobs[layer].data[0] print('done!') return indices, rep
def output_max_patches(max_tracker, net, layer, idx_begin, idx_end, num_top, datadir, filelist, outdir, do_which): do_maxes, do_deconv, do_deconv_norm, do_backprop, do_backprop_norm, do_info = do_which assert do_maxes or do_deconv or do_deconv_norm or do_backprop or do_backprop_norm or do_info, 'nothing to do' mt = max_tracker rc = RegionComputer() image_filenames, image_xmls, image_labels = load_file_list(filelist) print('Loaded filenames and labels for %d files' % len(image_filenames)) print(' First file', os.path.join(datadir, image_filenames[0])) num_top_in_mt = mt.max_locs.shape[1] assert num_top <= num_top_in_mt, 'Requested %d top images but MaxTracker contains only %d' % ( num_top, num_top_in_mt) assert idx_end >= idx_begin, 'Range error' size_ii, size_jj = get_max_data_extent(net, layer, rc, mt.is_conv) data_size_ii, data_size_jj = net.blobs['data'].data.shape[2:4] n_total_images = (idx_end - idx_begin) * num_top for cc, channel_idx in enumerate(range(idx_begin, idx_end)): unit_dir = os.path.join(outdir, layer, 'unit_%04d' % channel_idx) mkdir_p(unit_dir) if do_info: info_filename = os.path.join(unit_dir, 'info.txt') info_file = open(info_filename, 'w') info_file.write( '# is_conv val image_idx image_class i(if is_conv) j(if is_conv) filename' ) # iterate through maxes from highest (at end) to lowest for max_idx_0 in range(num_top): max_idx = num_top_in_mt - 1 - max_idx_0 if mt.is_conv: im_idx, im_class, ii, jj = mt.max_locs[channel_idx, max_idx] else: im_idx, im_class = mt.max_locs[channel_idx, max_idx] recorded_val = mt.max_vals[channel_idx, max_idx] filename = image_filenames[im_idx] roiname = image_xmls[im_idx] do_print = (max_idx_0 == 0) if do_print: print('%s Output file/image(s) %d/%d' % (datetime.now().ctime(), cc * num_top, n_total_images)) if mt.is_conv: # Compute the focus area of the data layer layer_indices = (ii, ii + 1, jj, jj + 1) data_indices = rc.convert_region(layer, 'data', layer_indices) data_ii_start, data_ii_end, data_jj_start, data_jj_end = data_indices touching_imin = (data_ii_start == 0) touching_jmin = (data_jj_start == 0) # Compute how much of the data slice falls outside the actual data [0,max] range ii_outside = size_ii - (data_ii_end - data_ii_start ) # possibly 0 jj_outside = size_jj - (data_jj_end - data_jj_start ) # possibly 0 if touching_imin: out_ii_start = ii_outside out_ii_end = size_ii else: out_ii_start = 0 out_ii_end = size_ii - ii_outside if touching_jmin: out_jj_start = jj_outside out_jj_end = size_jj else: out_jj_start = 0 out_jj_end = size_jj - jj_outside else: ii, jj = 0, 0 data_ii_start, out_ii_start, data_jj_start, out_jj_start = 0, 0, 0, 0 data_ii_end, out_ii_end, data_jj_end, out_jj_end = size_ii, size_ii, size_jj, size_jj if do_info: info_file.write(str(1 if mt.is_conv else 0)) info_file.write('%.6f' % mt.max_vals[channel_idx, max_idx]) if mt.is_conv: info_file.write('%d %d %d %d' % tuple(mt.max_locs[channel_idx, max_idx])) else: info_file.write('%d %d' % tuple(mt.max_locs[channel_idx, max_idx])) info_file.write(filename) if not (do_maxes or do_deconv or do_deconv_norm or do_backprop or do_backprop_norm): continue with WithTimer('Load image & ROIs', quiet=not do_print): im = load_image(os.path.join(datadir, filename)) _, _, rois = parse_roi_xml(os.path.join(datadir, roiname)) rois = np.array(rois) with WithTimer('Predict ', quiet=not do_print): net.blobs['data'].data[...] = im net.blobs['rois'].reshape(1, *rois.shape) net.blobs['rois'].data[...] = rois net.forward() if len(net.blobs[layer].data.shape) == 4: reproduced_val = net.blobs[layer].data[0, channel_idx, ii, jj] else: reproduced_val = net.blobs[layer].data[0, channel_idx] if abs(reproduced_val - recorded_val) > .1: print( 'Warning: recorded value %s is suspiciously different from reproduced value %s. Is the filelist the same?' % (recorded_val, reproduced_val)) if do_maxes: #grab image from data layer, not from im (to ensure preprocessing / center crop details match between image and deconv/backprop) out_arr = np.zeros((3, size_ii, size_jj), dtype='float32') out_arr[:, out_ii_start:out_ii_end, out_jj_start:out_jj_end] = net.blobs['data'].data[ 0, :, data_ii_start:data_ii_end, data_jj_start:data_jj_end] with WithTimer('Save img ', quiet=not do_print): save_caffe_image(out_arr, os.path.join(unit_dir, 'maxim_%03d.png' % max_idx_0), autoscale=False, autoscale_center=0) if do_deconv or do_deconv_norm: diffs = net.blobs[layer].diff * 0 if len(diffs.shape) == 4: diffs[0, channel_idx, ii, jj] = 1.0 else: diffs[0, channel_idx] = 1.0 with WithTimer('Deconv ', quiet=not do_print): net.deconv_from_layer(layer, diffs) out_arr = np.zeros((3, size_ii, size_jj), dtype='float32') out_arr[:, out_ii_start:out_ii_end, out_jj_start:out_jj_end] = net.blobs['data'].diff[ 0, :, data_ii_start:data_ii_end, data_jj_start:data_jj_end] if out_arr.max() == 0: print('Warning: Deconv out_arr in range', out_arr.min(), 'to', out_arr.max(), 'ensure force_backward: true in prototxt') if do_deconv: with WithTimer('Save img ', quiet=not do_print): save_caffe_image(out_arr, os.path.join( unit_dir, 'deconv_%03d.png' % max_idx_0), autoscale=False, autoscale_center=0) if do_deconv_norm: out_arr = np.linalg.norm(out_arr, axis=0) with WithTimer('Save img ', quiet=not do_print): save_caffe_image( out_arr, os.path.join(unit_dir, 'deconvnorm_%03d.png' % max_idx_0)) if do_backprop or do_backprop_norm: diffs = net.blobs[layer].diff * 0 diffs[0, channel_idx, ii, jj] = 1.0 with WithTimer('Backward ', quiet=not do_print): net.backward_from_layer(layer, diffs) out_arr = np.zeros((3, size_ii, size_jj), dtype='float32') out_arr[:, out_ii_start:out_ii_end, out_jj_start:out_jj_end] = net.blobs['data'].diff[ 0, :, data_ii_start:data_ii_end, data_jj_start:data_jj_end] if out_arr.max() == 0: print('Warning: Deconv out_arr in range', out_arr.min(), 'to', out_arr.max(), 'ensure force_backward: true in prototxt') if do_backprop: with WithTimer('Save img ', quiet=not do_print): save_caffe_image(out_arr, os.path.join( unit_dir, 'backprop_%03d.png' % max_idx_0), autoscale=False, autoscale_center=0) if do_backprop_norm: out_arr = np.linalg.norm(out_arr, axis=0) with WithTimer('Save img ', quiet=not do_print): save_caffe_image( out_arr, os.path.join(unit_dir, 'backpropnorm_%03d.png' % max_idx_0)) if do_info: info_file.close()
NOISE = 0 # Blur iterations. BLUR_ITERATIONS = 10000 MAX_BLUR = BLUR_ITERATIONS / 100 if __name__ == '__main__': LABELS = [] ITERATIONS = [] ERRORS = 0 FIRST_ERROR = 0 if NOISE is 1: # Noise loop. for i in range(NOISE_ITERATIONS): DISTORTED_IMAGE = add_noise(load_image(IMAGE_LOCATION), i / NOISE_ITERATIONS) #imwrite('distorted' + str(i) + '.png', DISTORTED_IMAGE) #print('Current noise amount: ', i / (NOISE_ITERATIONS * 2)) result, label = classify(MODEL_LOCATION, DISTORTED_IMAGE, RESULTS_LOCATION, False, True) if label is not IMAGE_LABEL: if FIRST_ERROR is 0: FIRST_ERROR = i / NOISE_ITERATIONS ERRORS = ERRORS + 1 LABELS.append(ERRORS) ITERATIONS.append(i / NOISE_ITERATIONS) if FIRST_ERROR is not 0: print('First error: ', FIRST_ERROR) # Plot.
def test_load_image(self): image = load_data.load_image('data/1000.jpg') self.assertEqual(image.shape, (3, 227, 227))
# Load point cloud pcl_xyz = ld.load_points_xyz('data/' + anns[i, 0]) pcl_rgb = ld.load_points_rgb('data/' + anns[i, 1]) pcl_sift = ld.load_points_sift('data/' + anns[i, 2]) # Load camera K, R, T, h, w = ld.load_camera('data/' + anns[i, 3]) proj_mat = K.dot(np.hstack((R, T))) # Project point cloud to camera pdepth, prgb, psift = ld.project_points(pcl_xyz, pcl_rgb, pcl_sift, proj_mat, h, w, prm.scale_size, prm.crop_size) simg = ld.scale_crop( ld.load_image('data/' + anns[i, 4]) / 127.5 - 1., prm.scale_size, prm.crop_size) gt_depth = ld.scale_crop(ld.load_depth_map('data/' + anns[i, 5], dtype=np.float16).astype( np.float32), prm.scale_size, prm.crop_size, is_depth=True) is_vis, is_val = ld.compute_visib_map(gt_depth, pdepth, pct_diff_thresh=5.) proj_depth.append((pdepth * is_val)[None, ...]) proj_sift.append((psift * is_val)[None, ...]) proj_rgb.append((prgb * is_val)[None, ...]) src_img.append(simg[None, ...]) gt_vis.append(is_vis[None, ...])
val = json.loads(f.read()) #with open("../marking_dict.json", "r") as f: # marking_dict = json.loads(f.read()) #print(val[:10]) #val = list(set(val).difference(set(marking_dict.keys()))) #val.remove("c8377613eae3302038e0f9844d2b3691cd20c529") #val.remove("cb96674e1a626d386ff96ceed15072c64bf837b6") #val.remove("b3f7aefa58fd4d2c1cbd107f1dc89b8f5604ae7d") #print(len(val)) #val.remove("193a1c95ee51356e1c02c229b9b83b2b57759f5e") #val.remove("10053d8e432a031b7610c9bd4549f99285afdde4") val_load = [val[i:i + args.batch] for i in range(0, len(val), args.batch)] print(val_load[0]) for batch_id, batch in tqdm(enumerate(val_load), mininterval=10): inputs = torch.stack( tuple([ load_image(args.img_store + i[:2] + "/" + i + ".jpg", args.width, args.height, args.resize, transform) for i in batch ]), 0).to(device) outputs = net(inputs) _, predicted = outputs.max(1) for i, elem in enumerate(batch): marking_dict[elem] = predicted[i].item() with open(args.marking_dict, "w") as f: json.dump(marking_dict, f)
def test_hog_scale(path): svc, scaler, dcspace, spatial_size, hist_bins, orient, pix_per_cell, cell_per_block, hog_channel, spatial_feat, hist_feat, hog_feat = load_classifier( ) #input_name = 'test1.jpg' #img = mpimg.imread(input_name) ystart = 400 ystop = 656 scale = 1.5 scspace = 'BGR' fnames = load_images(path) for fname in fnames: img = load_image(fname, scspace) print("processing", fname) heat = np.zeros_like(img[:, :, 0]).astype(np.float) for scale in (1.0, 1.5, 2.0): out_img, boxes = find_cars(img, scspace, dcspace, ystart, ystop, scale, svc, scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins, draw=True) add_heat(heat, boxes) # plt.imshow(out_img) basename = os.path.basename(fname) name, ext = os.path.splitext(basename) savename = os.path.join('output_images', name + "_subsample_" + str(scale) + ext) fig = plt.figure() plt.imshow(out_img) fig.savefig(savename) plt.close() heat = apply_threshold(heat, 1) heatmap = np.clip(heat, 0, 255) labels = label(heatmap) basename = os.path.basename(fname) name, ext = os.path.splitext(basename) savename = os.path.join('output_images', name + "_heatmap" + ext) fig = plt.figure() plt.imshow(heatmap, cmap='gray') fig.savefig(savename) plt.close() draw_labels = np.zeros_like(img[:, :, 0]).astype(np.uint8) draw_labels = draw_labeled_bboxes_solid(draw_labels, labels) savename = os.path.join('output_images', name + "_labels" + ext) fig = plt.figure() plt.imshow(draw_labels, cmap='gray') fig.savefig(savename) plt.close()
def process(k): print "image %d/%d (%s)" % (k + 1, num_images, subset) img_id = ids[k] img = load_data.load_image(img_id, from_ram=True, subset=subset) return estimate_params(img)
def test_color_hist(path): cars, noncars = load_training_images(path) car_ind = np.random.randint(0, len(cars)) noncar_ind = np.random.randint(0, len(noncars)) car_name = cars[car_ind] noncar_name = noncars[noncar_ind] for cspace in ('RGB', 'YUV', 'HLS', 'LUV', 'YCrCb', 'HSV', 'GRAY'): car_image = load_image(car_name, cspace) noncar_image = load_image(noncar_name, cspace) car_feature_vec, car_bincen, car_num_channels, car_ch0, car_ch1, car_ch2 = color_hist( car_image, nbins=32, bins_range=(0, 256)) noncar_feature_vec, noncar_bincen, noncar_num_channels, noncar_ch0, noncar_ch1, noncar_ch2 = color_hist( noncar_image, nbins=32, bins_range=(0, 256)) # Plot a figure with all channel bars if car_num_channels == 1: fig = plt.figure(figsize=(12, 6)) plt.subplot(231) plt.imshow(load_image(car_name, 'RGB')) plt.title(car_name) plt.subplot(232) plt.imshow(car_image, cmap='gray') plt.xlabel(cspace) plt.subplot(233) plt.bar(car_bincen, car_ch0[0]) plt.xlim(0, 256) plt.title('Histogram') plt.subplot(234) plt.imshow(load_image(noncar_name, 'RGB')) plt.title(noncar_name) plt.subplot(235) plt.imshow(noncar_image, cmap='gray') plt.xlabel(cspace) plt.subplot(236) plt.bar(noncar_bincen, noncar_ch0[0]) plt.xlim(0, 256) plt.title('Histogram') plt.suptitle(cspace) # fig.tight_layout() savename = os.path.join('output_images', cspace + '_hist.jpg') fig.savefig(savename) elif car_num_channels == 3: fig = plt.figure(figsize=(20, 6)) plt.suptitle(cspace) plt.subplot(271) plt.imshow(load_image(car_name, 'RGB')) plt.title(car_name) plt.subplot(272) plt.imshow(car_image[:, :, 0], cmap='gray') plt.xlabel('Channel 1') plt.subplot(273) plt.imshow(car_image[:, :, 1], cmap='gray') plt.xlabel('Channel 2') plt.subplot(274) plt.imshow(car_image[:, :, 2], cmap='gray') plt.xlabel('Channel 3') plt.subplot(275) plt.bar(car_bincen, car_ch0[0]) plt.xlim(0, 256) plt.xlabel('Channel 1 Histogram') plt.subplot(276) plt.bar(car_bincen, car_ch1[0]) plt.xlim(0, 256) plt.xlabel('Channel 2 Histogram') plt.subplot(277) plt.bar(car_bincen, car_ch2[0]) plt.xlim(0, 256) plt.xlabel('Channel 3 Histogram') plt.subplot(278) plt.imshow(load_image(noncar_name, 'RGB')) plt.title(noncar_name) plt.subplot(279) plt.imshow(noncar_image[:, :, 0], cmap='gray') plt.xlabel('Channel 1') plt.subplot(2, 7, 10) plt.imshow(noncar_image[:, :, 1], cmap='gray') plt.xlabel('Channel 2') plt.subplot(2, 7, 11) plt.imshow(noncar_image[:, :, 2], cmap='gray') plt.xlabel('Channel 3') plt.subplot(2, 7, 12) plt.bar(noncar_bincen, noncar_ch0[0]) plt.xlim(0, 256) plt.xlabel('Channel 1 Histogram') plt.subplot(2, 7, 13) plt.bar(noncar_bincen, noncar_ch1[0]) plt.xlim(0, 256) plt.xlabel('Channel 2 Histogram') plt.subplot(2, 7, 14) plt.bar(noncar_bincen, noncar_ch2[0]) plt.xlim(0, 256) plt.xlabel('Channel 3 Histogram') # fig.tight_layout() savename = os.path.join('output_images', cspace + '_hist.jpg') fig.savefig(savename) else: print( 'Your function is returning None for at least one variable...')
from load_data import load_data, load_data_split, load_image, load_labels, load_labels_split import sys import os test_dir = sys.argv[1] csv_dir = sys.argv[2] csv_name = os.path.join(csv_dir, 'predict.csv') h = 28 w = 28 num_classes = 10 X_test = load_image(test_dir) X_test = X_test.reshape(10000, h, w, 1) X_test = X_test.astype('float32') X_test /= 127.5 X_test -= 1 Y_train = load_labels() Y_train = keras.utils.to_categorical(Y_train, num_classes) model = load_model('2conv.h5') # model.save(save_model_path) predict_test = model.predict_classes(X_test) # print(predict_test)
def main(path): cars, noncars = load_training_images(path) car_ind = np.random.randint(0, len(cars)) noncar_ind = np.random.randint(0, len(noncars)) for spatial_color in ('RGB', 'YUV', 'LUV', 'YCrCb', 'HLS', 'HSV', 'GRAY'): for hist_color in ('RGB', 'YUV', 'LUV', 'YCrCb', 'HLS', 'HSV', 'GRAY'): car_features = extract_color_features(cars, spatial_color=spatial_color, spatial_size=(32, 32), hist_color=hist_color, hist_bins=32, hist_range=(0, 256)) notcar_features = extract_color_features( noncars, spatial_color=spatial_color, spatial_size=(32, 32), hist_color=hist_color, hist_bins=32, hist_range=(0, 256)) if len(car_features) > 0: # Create an array stack of feature vectors X = np.vstack( (car_features, notcar_features)).astype(np.float64) scaled_X, scaler = scale_norm(X) # Plot an example of raw and scaled features fig = plt.figure(figsize=(12, 8)) plt.subplot(231) plt.imshow(load_image(cars[car_ind], 'RGB')) plt.title('Original Image') plt.xlabel(cars[car_ind]) plt.subplot(232) plt.plot(X[car_ind]) plt.title('Raw Features') plt.subplot(233) plt.plot(scaled_X[car_ind]) plt.title('Normalized Features') plt.subplot(234) plt.imshow(load_image(noncars[noncar_ind], 'RGB')) plt.title('Original Image') plt.xlabel(noncars[noncar_ind]) plt.subplot(235) plt.plot(X[noncar_ind + len(cars)]) plt.title('Raw Features') plt.subplot(236) plt.plot(scaled_X[noncar_ind + len(cars)]) plt.title('Normalized Features') plt.suptitle('spatial color: ' + spatial_color + ' histogram color: ' + hist_color) fig.tight_layout() savename = os.path.join( 'output_images', spatial_color + '_' + hist_color + '_feature.jpg') fig.savefig(savename) plt.close() else: print('Your function only returns empty feature vectors...')
def load_and_process_image_brightness_norm(img_index, ds_transforms, augmentation_params, target_sizes=None): img_id = load_data.train_ids[img_index] img = load_data.load_image(img_id, from_ram=True) img = img / img.max() # normalise img_a = perturb_and_dscrop(img, ds_transforms, augmentation_params, target_sizes) return img_a