def purifySets(): sets = { const.train: os.path.join(Path.sets, extendName(const.train, Extensions.txt)), const.valid: os.path.join(Path.sets, extendName(const.valid, Extensions.txt)), const.test: os.path.join(Path.sets, extendName(const.test, Extensions.txt)), } for set_, path in sets.items(): files = readLines(path) total = len(files) files = [f for f in files if os.path.exists(f)] writeLines(files, path) print(f"Cleaned {total - len(files)} from {path}")
def extractMarks(categoryDir): marksPath = os.path.join(categoryDir, makeJSONname(const.marks)) framesDir = os.path.join(categoryDir, const.frames) try: marks = json.load(open(marksPath, "r")) except FileNotFoundError: return print( f"\n{Fore.GREEN}Processing extraction marks for {marksPath} {Style.RESET_ALL}" ) for frameIdx, frameName in enumerate(marks): frameMarks = marks[frameName] framePath = os.path.join(framesDir, frameMarks[const.image]) if not os.path.exists(framePath): continue y1, x1, y2, x2 = frameMarks[const.coords] ctgIdx = frameMarks[const.ctgIdx] h, w = frameMarks[const.imageShape] xc = (x2 + x1) / (2 * w) yc = (y2 + y1) / (2 * h) bw = (x2 - x1) / w bh = (y2 - y1) / h darknetString = f"{ctgIdx} {xc} {yc} {bw} {bh}\n" if not checkBoundingBoxIsCorrect(bw, bh): darknetString = "" txtName = os.path.splitext(frameMarks['image'])[0] with open(os.path.join(framesDir, extendName(txtName, Extensions.txt)), "w") as f: f.write(darknetString) print("\r{:.1f}% of work has been done".format( (frameIdx + 1) / len(marks) * 100), end="")
def augmentCategoryWithRepeats(categoryPath, fullCategory, augmentPath, augmentations, extension=Extensions.jpg, repeats=1, params=None): print(f"Category {fullCategory} is being augmented") if repeats == 0: print( f"{Fore.RED}Too many original images for {categoryPath}, aborting augmentation {Style.RESET_ALL}" ) return marksName = makeJSONname(const.marks) marksPath = os.path.join(categoryPath, marksName) framesPath = os.path.join(categoryPath, const.frames) augmentedCategoryPath = os.path.join(augmentPath, *splitFullCategory(fullCategory)) try: marks = json.load(open(marksPath, "r")) except: print( f"{Fore.RED}There is no marks {marksPath} for frames in {categoryPath} {Style.RESET_ALL}" ) return idx = 0 augmentedMarks = {} for i, name in enumerate(marks): print("\r{:.1f}% of work has been done".format( (i + 1) / len(marks) * 100), end="") frameData = marks[name] frameName = frameData[const.image] box = frameData[const.coords] ctgIdx = frameData[const.ctgIdx] shape = frameData[const.imageShape] frameID = name.split(const.separator)[1] image = cv2.imread(os.path.join(framesPath, frameName)) augmented = augmentImageRepeated(image=image, augmentations=augmentations, repeats=repeats, boxes=[box]) augmentedFramesPath = os.path.join(augmentedCategoryPath, const.frames) os.makedirs(augmentedFramesPath, exist_ok=True) for augImage, augBox in augmented: augmentedName = f"{fullCategory}{const.separator}{frameID}_{idx}{const.separator}{const.augmented}" augmentedFileName = extendName(augmentedName, extension) augmentedMarks[augmentedName] = { const.image: augmentedFileName, const.coords: augBox, const.fullCategory: fullCategory, const.ctgIdx: ctgIdx, const.imageShape: shape } cv2.imwrite(os.path.join(augmentedFramesPath, augmentedFileName), augImage, params) idx += 1 print() json.dump(augmentedMarks, open(os.path.join(augmentedCategoryPath, marksName), "w"), indent=3) print( f"{Fore.GREEN}Category {fullCategory} has been successfully augmented. " f"Results in {augmentedCategoryPath} {Style.RESET_ALL}")
def augmentCategoryWithGenerator(categoryPath, fullCategory, augmentPath, augmentations, augmentationsNumber, extension=Extensions.jpg, params=None): print('category: {:>50} \t process_id: {:>10} \t process_name: {}'.format( fullCategory, os.getpid(), mp.current_process())) time.sleep(0.5) augmentations = customAugmentations if augmentations is None else augmentations # хардкод для запуска мультипроцессинга # print(f"Category {fullCategory} is being augmented") if augmentationsNumber == 0: print( f"{Fore.RED}No augmentations for {categoryPath}{Style.RESET_ALL}") return marksName = makeJSONname(const.marks) marksPath = os.path.join(categoryPath, marksName) framesPath = os.path.join(categoryPath, const.frames) augmentedCategoryPath = os.path.join(augmentPath, *splitFullCategory(fullCategory)) try: marks = json.load(open(marksPath, "r")) except: print( f"{Fore.RED}There is no marks {marksPath} for frames in {categoryPath} {Style.RESET_ALL}" ) return augGenerator = augmentationGenerator(framesPath, marks, augmentations, augmentationsNumber) augmentedFramesPath = os.path.join(augmentedCategoryPath, const.frames) os.makedirs(augmentedFramesPath, exist_ok=True) augmentedMarks = {} for i, aug in enumerate(augGenerator): print("\r{} {:.1f} is ready".format(fullCategory, i / augmentationsNumber * 100), end="") augFrame, augFrameData = aug augmentedName = augFrameData.pop(const.image) augmentedFileName = extendName(augmentedName, extension) augFrameData[const.image] = augmentedFileName cv2.imwrite(os.path.join(augmentedFramesPath, augmentedFileName), augFrame, params) augmentedMarks[augmentedName] = augFrameData print() json.dump(augmentedMarks, open(os.path.join(augmentedCategoryPath, marksName), "w"), indent=3) print( f"\n{Fore.GREEN}Category {fullCategory} has been successfully augmented. " f"Results in {augmentedCategoryPath} {Style.RESET_ALL}")
def makeSets(directories, wpath=Path.sets, trainPart=0.9, validPart=0.05, ignoreOld=False, matchWithMarks=True): assert 0 < trainPart + validPart <= 1 os.makedirs(wpath, exist_ok=True) testPart = 1 - trainPart - validPart sets = { const.train: { "path": os.path.join(wpath, extendName(const.train, Extensions.txt)), "part": trainPart, "content": [] }, const.valid: { "path": os.path.join(wpath, extendName(const.valid, Extensions.txt)), "part": validPart, "content": [] }, const.test: { "path": os.path.join(wpath, extendName(const.test, Extensions.txt)), "part": testPart, "content": [] } } inUse = [] for set_, info in sets.items(): info["content"] = readLines(info["path"]) if not ignoreOld else [] inUse.extend(info["content"]) images = [] marks = [] for dirIdx, path in enumerate(directories): print( "\rSearching for images and marks in listed directories, {:.1f}% has been done" .format(dirIdx / len(directories) * 100), end="") dirImages = [ os.path.join(path, *img) for img in walk( path, targetExtensions=Extensions.images()).get("extensions") ] images.extend(dirImages) if matchWithMarks: dirMarks = [ os.path.join(path, *mrk) for mrk in walk( path, targetExtensions=Extensions.txt).get("extensions") ] marks.extend(dirMarks) if matchWithMarks: transformer = lambda x: changeExtension(x, Extensions.txt) print("Matching images to marks, please wait...") images = matchLists(master=marks, slave=images, transformer=transformer) # _, images = matchLists(master=inUse, slave=images, getMismatched=True) images = permutate(images) start = 0 for set_, info in sets.items(): part = info["part"] end = start + int(part * len(images)) total = end - start info["content"].extend(images[start:end]) info["content"] = permutate(info["content"]) start = end writeLines(lines=info["content"], path=info["path"]) print(f"\n{Fore.GREEN}Added {total} paths to {set_} {Style.RESET_ALL}")
def main(argv): # Init cfg file cfg_file = '' # Get parameters try: opts, _ = getopt.getopt(argv, "h:", ["cfg="]) except getopt.GetoptError: dispHelp() return for opt, arg in opts: if opt == '-h': dispHelp(argv[0]) return elif opt in ("--cfg"): cfg_file = arg print "Loading configuration file: ", cfg_file (dataset, im_folder, im_list_file, output_file, feature_file_path, dot_ending, pw_base, pw_norm, pw_dens, sigmadots, Nr, n_scales, split_size, do_flip, perspective_path, use_perspective, is_colored, resize_im) = initGenFeatFromCfg(cfg_file) print "Choosen parameters:" print "-------------------" print "Dataset: ", dataset print "Data base location: ", im_folder print "Image names file: ", im_list_file print "Output file:", output_file print "Output feature names file:", feature_file_path print "Dot image ending: ", dot_ending print "Patch width (pw_base): ", pw_base print "Patch width (pw_norm): ", pw_norm print "Patch width (pw_dens): ", pw_dens print "Number of patches per image: ", Nr print "Perspective map: ", perspective_path print "Use perspective:", use_perspective print "Sigma for each dot: ", sigmadots print "Number of scales: ", n_scales print "Split size: ", split_size print "Flip images: ", do_flip print "Resize images: ", resize_im print "===================" print "Reading perspective file" if use_perspective: pers_file = h5py.File(perspective_path, 'r') pmap = np.array(pers_file['pmap']) pers_file.close() print "Creating feature names file:" feature_file = open(feature_file_path, 'w') feature_file.close() # Create empty file print "Reading image file names:" im_names = np.loadtxt(im_list_file, dtype='str') ldens = [] lpos = [] lpatches = [] file_count = 0 for ix, name in enumerate(im_names): print "Processing image: ", name # Get image paths im_path = utl.extendName(name, im_folder) dot_im_path = utl.extendName(name, im_folder, use_ending=True, pattern=dot_ending) # Read image files im = loadImage(im_path, color=is_colored) dot_im = loadImage(dot_im_path, color=True) # Do ground truth if use_perspective: dens_im = genPDensity(dot_im, sigmadots, pmap) else: dens_im = genDensity(dot_im, sigmadots) if resize_im > 0: # Resize image im = utl.resizeMaxSize(im, resize_im) gt_sum = dens_im.sum() dens_im = utl.resizeMaxSize(dens_im, resize_im) dens_im = dens_im * gt_sum / dens_im.sum() # Collect features from random locations # height, width, _ = im.shape # pos = get_dense_pos(height, width, pw_base=pw_base, stride = 5 ) pos = utl.genRandomPos(im.shape, pw_base, Nr) # Collect original patches patch = cropAtPos(im, pos, pw_base) # patch = cropPerspective(im, pos, pmap, pw_base) # Collect dens patches dpatch = cropAtPos(dens_im, pos, pw_base) # dpatch = cropPerspective(dens_im, pos, pmap, pw_base) # Resize images patch = utl.resizePatches(patch, (pw_norm, pw_norm)) dpatch = utl.resizeListDens( dpatch, (pw_dens, pw_dens)) # 18 is the output size of the paper # Flip function if do_flip: fpatch = hFlipImages(patch) fdpatch = hFlipImages(dpatch) fscales = extractEscales(fpatch, n_scales) # Add flipped data lpatches.append(fscales) ldens.append(fdpatch) lpos.append(pos) # Store features and densities ldens.append(dpatch) lpos.append(pos) lpatches.append(extractEscales(patch, n_scales)) # Save it into a file if split_size > 0 and (ix + 1) % split_size == 0: # Prepare for saving ldens = np.vstack(ldens) lpos = np.vstack(lpos) patches_list = np.vstack(lpatches[:]) opt_num_name = output_file + str(file_count) + ".h5" print "Saving data file: ", opt_num_name print "Saving {} examples".format(len(ldens)) # Compress data and save feature_file = open(feature_file_path, 'a') comp_kwargs = {'compression': 'gzip', 'compression_opts': 1} with h5py.File(opt_num_name, 'w') as f: f.create_dataset('label', data=ldens, **comp_kwargs) # Save all scales data for s in range(n_scales): dataset_name = 'data_s{}'.format(s) print "Creating dataset: ", dataset_name f.create_dataset(dataset_name, data=trasposeImages(patches_list[:, s, ...]), **comp_kwargs) f.close() feature_file.write(opt_num_name + '\n') feature_file.close() # Increase file counter file_count += 1 # Clean memory ldens = [] lpos = [] lpatches = [] ## Last save if len(lpatches) > 0: # Prepare for saving ldens = np.vstack(ldens) lpos = np.vstack(lpos) patches_list = np.vstack(lpatches[:]) opt_num_name = output_file + ".h5" print "Saving data file: ", opt_num_name print "Saving {} examples".format(len(ldens)) # Compress data and save feature_file = open(feature_file_path, 'a') comp_kwargs = {'compression': 'gzip', 'compression_opts': 1} with h5py.File(opt_num_name, 'w') as f: f.create_dataset('label', data=ldens, **comp_kwargs) # Save all scales data for s in range(n_scales): dataset_name = 'data_s{}'.format(s) print "Creating dataset: ", dataset_name f.create_dataset(dataset_name, data=trasposeImages(patches_list[:, s, ...]), **comp_kwargs) f.close() feature_file.write(opt_num_name + '\n') feature_file.close() print "--------------------" print "Finish!"
def main(argv): # Init parameters use_cpu = False gpu_dev = 0 # GAME max level mx_game = 4 # Max game target # Batch size b_size = -1 # CNN vars prototxt_path = 'models/trancos/hydra2/hydra_deploy.prototxt' caffemodel_path = 'models/trancos/hydra2/trancos_hydra2.caffemodel' # Get parameters try: opts, _ = getopt.getopt( argv, "h:", ["prototxt=", "caffemodel=", "cpu_only", "dev=", "cfg="]) except getopt.GetoptError as err: print "Error while parsing parameters: ", err dispHelp(argv[0]) return for opt, arg in opts: if opt == '-h': dispHelp(argv[0]) return elif opt in ("--prototxt"): prototxt_path = arg elif opt in ("--caffemodel"): caffemodel_path = arg elif opt in ("--cpu_only"): use_cpu = True elif opt in ("--dev"): gpu_dev = int(arg) elif opt in ("--cfg"): cfg_file = arg print "Loading configuration file: ", cfg_file (dataset, use_mask, mask_file, test_names_file, im_folder, dot_ending, pw, sigmadots, n_scales, perspective_path, use_perspective, is_colored, results_file, resize_im) = initTestFromCfg(cfg_file) print "Choosen parameters:" print "-------------------" print "Use only CPU: ", use_cpu print "GPU devide: ", gpu_dev print "Dataset: ", dataset print "Results files: ", results_file print "Test data base location: ", im_folder print "Test inmage names: ", test_names_file print "Dot image ending: ", dot_ending print "Use mask: ", use_mask print "Mask pattern: ", mask_file print "Patch width (pw): ", pw print "Sigma for each dot: ", sigmadots print "Number of scales: ", n_scales print "Perspective map: ", perspective_path print "Use perspective:", use_perspective print "Prototxt path: ", prototxt_path print "Caffemodel path: ", caffemodel_path print "Batch size: ", b_size print "Resize images: ", resize_im print "===================" print "----------------------" print "Preparing for Testing" print "======================" # Set GPU CPU setting if use_cpu: caffe.set_mode_cpu() else: # Use GPU caffe.set_device(gpu_dev) caffe.set_mode_gpu() print "Reading perspective file" if use_perspective: pers_file = h5py.File(perspective_path, 'r') pmap = np.array(pers_file['pmap']) pers_file.close() mask = None if dataset == 'UCSD': print "Reading mask" if use_mask: mask_f = h5py.File(mask_file, 'r') mask = np.array(mask_f['mask']) mask_f.close() print "Reading image file names:" im_names = np.loadtxt(test_names_file, dtype='str') # Perform test ntrueall = [] npredall = [] # Init GAME n_im = len(im_names) game_table = np.zeros((n_im, mx_game)) # Init CNN CNN = CaffePredictor(prototxt_path, caffemodel_path, n_scales) print print "Start prediction ..." count = 0 gt_vector = np.zeros((len(im_names))) pred_vector = np.zeros((len(im_names))) for ix, name in enumerate(im_names): # Get image paths im_path = utl.extendName(name, im_folder) dot_im_path = utl.extendName(name, im_folder, use_ending=True, pattern=dot_ending) # Read image files im = loadImage(im_path, color=is_colored) dot_im = loadImage(dot_im_path, color=True) # Generate features if use_perspective: dens_im = genPDensity(dot_im, sigmadots, pmap) else: dens_im = genDensity(dot_im, sigmadots) if resize_im > 0: # Resize image im = utl.resizeMaxSize(im, resize_im) gt_sum = dens_im.sum() dens_im = utl.resizeMaxSize(dens_im, resize_im) dens_im = dens_im * gt_sum / dens_im.sum() # Get mask if needed if dataset != 'UCSD': if use_mask: mask_im_path = utl.extendName(name, im_folder, use_ending=True, pattern=mask_file) mask = sio.loadmat(mask_im_path, chars_as_strings=1, matlab_compatible=1) mask = mask.get('BW') s = time.time() ntrue, npred, resImg, gtdots = testOnImg(CNN, im, dens_im, pw, mask) print "image : %d , ntrue = %.2f ,npred = %.2f , time =%.2f sec" % ( count, ntrue, npred, time.time() - s) # Keep individual predictions gt_vector[ix] = ntrue pred_vector[ix] = npred # Hold predictions and originasl ntrueall.append(ntrue) npredall.append(npred) # Compute game metric for l in range(mx_game): game_table[count, l] = gameMetric(resImg, gtdots, l) count = count + 1 ntrueall = np.asarray(ntrueall) npredall = np.asarray(npredall) print "done ! mean absolute error %.2f" % np.mean( np.abs(ntrueall - npredall)) # Print Game results results = np.zeros(mx_game) for l in range(mx_game): results[l] = np.mean(game_table[:, l]) print "GAME for level %d: %.2f " % (l, np.mean(game_table[:, l])) # Dump results into a txt file np.savetxt(results_file + '_pred.txt', npredall) np.savetxt(results_file + '_gt.txt', ntrueall) return 0
def frameVideo(filePath, marksPath, datasetPath, actualInfo, overwrite=False, extension=Extensions.jpg, params=None, ctgLimit=None): categories = readLines(Path.categories) basename = extractBasename(filePath) try: jsonName = makeJSONname(basename) marks = json.load(open(os.path.join(marksPath, jsonName), "r")) except: print( f"{Fore.RED}There is no json file {marksPath} for {filePath} {Style.RESET_ALL}" ) return framesGenerator = generateFrames(filePath) offset = getKeysOffset(marks.keys()) marksSeparated = {} total = 0 for idx, frame in enumerate(framesGenerator): # if idx == 20: # break frameMarks = getFrameMarks(idx, marks, offset) if not frameMarks: continue category = frameMarks[const.category] subcategory = frameMarks[const.subcategory] countKeys = [const.original, category, subcategory] if idx == 0: globalIdx = getNested(dictionary=actualInfo, keys=countKeys, default=0) localIdx = idx + globalIdx if ctgLimit is not None and localIdx == ctgLimit: break frameID = f"frame_{localIdx}" fullCategory = getFullCategory(category, subcategory) if fullCategory not in categories: categories.append(fullCategory) ctgIdx = categories.index(fullCategory) frameName = f"{fullCategory}{const.separator}{frameID}{const.separator}{const.original}" dirPath = os.path.join(datasetPath, const.original, category, subcategory) framesPath = os.path.join(dirPath, const.frames) framePath = os.path.join(framesPath, extendName(frameName, extension)) updateNested(dictionary=actualInfo, keys=countKeys, value=1) if not overwrite and os.path.exists(framePath): print("\rFrame #{} has been passed".format(idx), end="") continue os.makedirs(framesPath, exist_ok=True) frameInfo = { const.image: extendName(frameName, extension), const.coords: fitCoords(frameMarks[const.coords], frame.shape[:2]), const.fullCategory: fullCategory, const.ctgIdx: ctgIdx, const.imageShape: frame.shape[:2] } keySet = countKeys + [ frameName ] # ["original", category, subcategory, frameName] putNested(dictionary=marksSeparated, keys=keySet, value=frameInfo) cv2.imwrite(framePath, frame, params) total += 1 print("\rFrame #{} has been added".format(idx), end="") marksSeparated = marksSeparated[const.original] print() for ctg, value in marksSeparated.items(): for subctg, subctgMarks in value.items(): subctgMarksJson = os.path.join( datasetPath, const.original, ctg, subctg, extendName(const.marks, Extensions.json)) oldMarks = openJsonSafely(subctgMarksJson) for k, v in subctgMarks.items(): oldMarks[k] = v json.dump(oldMarks, open(subctgMarksJson, "w"), indent=3) print( f"{Fore.GREEN}Added marks to {subctgMarksJson} {Style.RESET_ALL}" ) writeLines(categories, Path.categories) print( f"{Fore.GREEN}Updated categories file {Path.categories} {Style.RESET_ALL}" ) print(f"{Fore.GREEN}Added {total} frames in total {Style.RESET_ALL}")
def main(argv): # Init cfg file cfg_file = '' # Get parameters try: opts, _ = getopt.getopt(argv, "h:", ["cfg="]) except getopt.GetoptError: dispHelp() return for opt, arg in opts: if opt == '-h': dispHelp(argv[0]) return elif opt in ("--cfg"): cfg_file = arg print "Loading configuration file: ", cfg_file (dataset, im_folder, im_list_file, output_file, feature_file_path, dot_ending, pw_base, pw_norm, pw_dens, sigmadots, Nr, n_scales, split_size, do_flip, perspective_path, use_perspective, is_colored, resize_im) = initGenFeatFromCfg(cfg_file) print "Choosen parameters:" print "-------------------" print "Dataset: ", dataset print "Data base location: ", im_folder print "Image names file: ", im_list_file print "Output file:", output_file print "Output feature names file:", feature_file_path print "Dot image ending: ", dot_ending print "Patch width (pw_base): ", pw_base print "Patch width (pw_norm): ", pw_norm print "Patch width (pw_dens): ", pw_dens print "Number of patches per image: ", Nr print "Perspective map: ", perspective_path print "Use perspective:", use_perspective print "Sigma for each dot: ", sigmadots print "Number of scales: ", n_scales print "Split size: ", split_size print "Flip images: ", do_flip print "Resize images: ", resize_im print "===================" print "Reading perspective file" if use_perspective: pers_file = h5py.File(perspective_path,'r') pmap = np.array( pers_file['pmap'] ) pers_file.close() print "Creating feature names file:" feature_file = open(feature_file_path, 'w') feature_file.close() # Create empty file print "Reading image file names:" im_names = np.loadtxt(im_list_file, dtype='str') ldens = [] lpos = [] lpatches = [] file_count = 0 for ix, name in enumerate(im_names): print "Processing image: ", name # Get image paths im_path = utl.extendName(name, im_folder) dot_im_path = utl.extendName(name, im_folder, use_ending=True, pattern=dot_ending) # Read image files im = loadImage(im_path, color = is_colored) dot_im = loadImage(dot_im_path, color = True) # Do ground truth if use_perspective: dens_im = genPDensity(dot_im, sigmadots, pmap) else: dens_im = genDensity(dot_im, sigmadots) if resize_im > 0: # Resize image im = utl.resizeMaxSize(im, resize_im) gt_sum = dens_im.sum() dens_im = utl.resizeMaxSize(dens_im, resize_im) dens_im = dens_im * gt_sum / dens_im.sum() # Collect features from random locations # height, width, _ = im.shape # pos = get_dense_pos(height, width, pw_base=pw_base, stride = 5 ) pos = utl.genRandomPos(im.shape, pw_base, Nr) # Collect original patches patch = cropAtPos(im, pos, pw_base) # patch = cropPerspective(im, pos, pmap, pw_base) # Collect dens patches dpatch = cropAtPos(dens_im, pos, pw_base) # dpatch = cropPerspective(dens_im, pos, pmap, pw_base) # Resize images patch = utl.resizePatches(patch, (pw_norm,pw_norm)) dpatch = utl.resizeListDens(dpatch, (pw_dens, pw_dens)) # 18 is the output size of the paper # Flip function if do_flip: fpatch = hFlipImages(patch) fdpatch = hFlipImages(dpatch) fscales = extractEscales(fpatch, n_scales) # Add flipped data lpatches.append( fscales ) ldens.append( fdpatch ) lpos.append( pos ) # Store features and densities ldens.append( dpatch ) lpos.append(pos) lpatches.append( extractEscales(patch, n_scales) ) # Save it into a file if split_size > 0 and (ix + 1) % split_size == 0: # Prepare for saving ldens = np.vstack(ldens) lpos = np.vstack(lpos) patches_list = np.vstack(lpatches[:]) opt_num_name = output_file + str(file_count) + ".h5" print "Saving data file: ", opt_num_name print "Saving {} examples".format(len(ldens)) # Compress data and save feature_file = open(feature_file_path, 'a') comp_kwargs = {'compression': 'gzip', 'compression_opts': 1} with h5py.File(opt_num_name, 'w') as f: f.create_dataset('label', data=ldens, **comp_kwargs) # Save all scales data for s in range(n_scales): dataset_name = 'data_s{}'.format(s) print "Creating dataset: ", dataset_name f.create_dataset(dataset_name, data=trasposeImages(patches_list[:,s,...]), **comp_kwargs) f.close() feature_file.write(opt_num_name + '\n') feature_file.close() # Increase file counter file_count += 1 # Clean memory ldens = [] lpos = [] lpatches = [] ## Last save if len(lpatches) >0: # Prepare for saving ldens = np.vstack(ldens) lpos = np.vstack(lpos) patches_list = np.vstack(lpatches[:]) opt_num_name = output_file + ".h5" print "Saving data file: ", opt_num_name print "Saving {} examples".format(len(ldens)) # Compress data and save feature_file = open(feature_file_path, 'a') comp_kwargs = {'compression': 'gzip', 'compression_opts': 1} with h5py.File(opt_num_name, 'w') as f: f.create_dataset('label', data=ldens, **comp_kwargs) # Save all scales data for s in range(n_scales): dataset_name = 'data_s{}'.format(s) print "Creating dataset: ", dataset_name f.create_dataset(dataset_name, data=trasposeImages(patches_list[:,s,...]), **comp_kwargs) f.close() feature_file.write(opt_num_name + '\n') feature_file.close() print "--------------------" print "Finish!"
def extract(ctg, ctgInfo, videosPath=Path.rawVideos, extractionPath=Path.original, extension=Extensions.jpg, limit=None, augmentFunc=None, augmentations=None, augmentationName=const.augmented, augmentationPath=None, overwriteOriginal=False, overwriteAugmented=True): try: parent = ctgInfo.get(const.parent, "") fullExtractionPath = os.path.join(extractionPath, parent, ctg) os.makedirs(os.path.join(fullExtractionPath, const.frames), exist_ok=True) videos = ctgInfo[const.videos] overall = ctgInfo[const.overall] limit = limit if limit is not None else overall if augmentFunc is not None: augmentFunc = proxifyAugmentFunc(augmentFunc) augmentations = int( augmentations) if augmentations is not None else min( limit, overall) augmentations = max(augmentations, augmentations + limit - overall) augRepeats = ceil(augmentations / min(limit, overall)) augmentationPath = augmentationPath if augmentationPath is not None \ else extractionPath.replace(const.original, augmentationName) fullAugmentationPath = os.path.join(augmentationPath, parent, ctg) os.makedirs(os.path.join(fullAugmentationPath, const.frames), exist_ok=True) existingAugs = len( os.listdir(os.path.join(fullAugmentationPath, const.frames))) augMarks = {} totalAugs = 0 fullCategory = getFullCategory(parent, ctg) print( "Cutting videos: {:>50} \t expected orig frames {:>10} \t expected aug frames \t {:>10} process id: {:>10}" .format(fullCategory, min(limit, overall), augmentations, os.getpid())) sleep(0.5) # time.sleep(0.5) generator = createGenerator(videosPath, videos, overall, limit) marks = {} total = 0 for idx, genInfo in enumerate(generator): frame, frameName, coords = genInfo fullFrameName = const.separator.join( (fullCategory, frameName, const.original)) framePath = os.path.join(fullExtractionPath, const.frames, extendName(fullFrameName, extension)) coords = fitCoords(coords, frame.shape[:2]) status = "passed" if not os.path.exists(framePath) or overwriteOriginal: status = "added" frameMarks = { const.fullCategory: fullCategory, const.ctgIdx: ctgInfo[const.ctgIdx], const.image: extendName(fullFrameName, extension), const.coords: coords, const.imageShape: frame.shape[:2] } cv2.imwrite(framePath, frame) marks[frameName] = frameMarks total += 1 if augmentFunc is not None: frameAugments = 0 for i in range(augRepeats): augFrameName = f"{fullCategory}{const.separator}{frameName}_{i}{const.separator}{augmentationName}" augFramePath = os.path.join( fullAugmentationPath, const.frames, extendName(augFrameName, extension)) if totalAugs >= augmentations or ( existingAugs >= augmentations and not overwriteAugmented): break if os.path.exists(augFramePath) and not overwriteAugmented: continue augFrame, augCoords = augmentFunc(frame, coords) augFrameMarks = { const.fullCategory: fullCategory, const.image: extendName(augFrameName, extension), const.ctgIdx: ctgInfo[const.ctgIdx], const.coords: fitCoords(augCoords, augFrame.shape[:2]), const.imageShape: augFrame.shape[:2] } cv2.imwrite(augFramePath, augFrame) augMarks[augFrameName] = augFrameMarks frameAugments += 1 totalAugs += frameAugments print("\rFrame #{} has been {} with {} augmentations".format( idx + 1, status, frameAugments), end="") marksPath = os.path.join(fullExtractionPath, makeJSONname(const.marks)) oldMarks = openJsonSafely(marksPath) json.dump(updateMarks(oldMarks, marks, overwriteOriginal), open(marksPath, "w"), indent=3, sort_keys=True) print( f"\n{Fore.GREEN}Added marks to {fullExtractionPath} {Style.RESET_ALL}" ) if augmentFunc is not None: augMarksPath = os.path.join(fullAugmentationPath, makeJSONname(const.marks)) oldAugMarks = openJsonSafely(augMarksPath) json.dump(updateMarks(oldAugMarks, augMarks, overwriteAugmented), open(augMarksPath, "w"), indent=3, sort_keys=True) print( f"{Fore.GREEN}Added marks to {fullAugmentationPath} {Style.RESET_ALL}" ) print( f"{Fore.GREEN}Added {total} pure frames and {totalAugs} augmented frames in total {Style.RESET_ALL}" ) except Exception as e: print(e)
def main(argv, image_name): use_cpu = False gpu_dev = 0 prototxt_path = 'models/trancos/hydra2/hydra_deploy.prototxt' caffemodel_path = 'models/trancos/hydra2/trancos_hydra2.caffemodel' try: opts, _ = getopt.getopt( argv, "h:", ["prototxt=", "caffemodel=", "cpu_only", "dev=", "cfg="]) except getopt.GetoptError as err: print("Error while parsing parameters: ", err) return for opt, arg in opts: if opt in ("--prototxt"): prototxt_path = arg elif opt in ("--caffemodel"): caffemodel_path = arg elif opt in ("--cpu_only"): use_cpu = True elif opt in ("--dev"): gpu_dev = int(arg) elif opt in ("--cfg"): cfg_file = arg (dataset, use_mask, mask_file, test_names_file, im_folder, dot_ending, pw, sigmadots, n_scales, perspective_path, use_perspective, is_colored, results_file, resize_im) = init_parameters_from_config(cfg_file) if use_cpu: caffe.set_mode_cpu() else: # Use GPU caffe.set_device(gpu_dev) caffe.set_mode_gpu() # Init CNN CNN = CaffePredictor(prototxt_path, caffemodel_path, n_scales) print("\nStart prediction for " + image_name) im_path = utl.extendName(image_name, im_folder) im = load_image(im_path, color=is_colored) if resize_im > 0: im = utl.resizeMaxSize(im, resize_im) mask = None if use_mask: mask_im_path = utl.extendName(image_name, im_folder, use_ending=True, pattern=mask_file) mask = sio.loadmat(mask_im_path, chars_as_strings=1, matlab_compatible=1) mask = mask.get('BW') s = time.time() npred, resImg = count_objects(CNN, im, pw, mask) print("image : %s, npred = %.2f , time =%.2f sec" % (image_name, npred, time.time() - s)) return npred
def main(argv): # Init parameters use_cpu = False gpu_dev = 0 # GAME max level mx_game = 4 # Max game target # Batch size b_size = -1 # CNN vars prototxt_path = 'models/trancos/hydra2/hydra_deploy.prototxt' caffemodel_path = 'models/trancos/hydra2/trancos_hydra2.caffemodel' # Get parameters try: opts, _ = getopt.getopt(argv, "h:", ["prototxt=", "caffemodel=", "cpu_only", "dev=", "cfg="]) except getopt.GetoptError as err: print "Error while parsing parameters: ", err dispHelp(argv[0]) return for opt, arg in opts: if opt == '-h': dispHelp(argv[0]) return elif opt in ("--prototxt"): prototxt_path = arg elif opt in ("--caffemodel"): caffemodel_path = arg elif opt in ("--cpu_only"): use_cpu = True elif opt in ("--dev"): gpu_dev = int(arg) elif opt in ("--cfg"): cfg_file = arg print "Loading configuration file: ", cfg_file (dataset, use_mask, mask_file, test_names_file, im_folder, dot_ending, pw, sigmadots, n_scales, perspective_path, use_perspective, is_colored, results_file, resize_im) = initTestFromCfg(cfg_file) print "Choosen parameters:" print "-------------------" print "Use only CPU: ", use_cpu print "GPU devide: ", gpu_dev print "Dataset: ", dataset print "Results files: ", results_file print "Test data base location: ", im_folder print "Test inmage names: ", test_names_file print "Dot image ending: ", dot_ending print "Use mask: ", use_mask print "Mask pattern: ", mask_file print "Patch width (pw): ", pw print "Sigma for each dot: ", sigmadots print "Number of scales: ", n_scales print "Perspective map: ", perspective_path print "Use perspective:", use_perspective print "Prototxt path: ", prototxt_path print "Caffemodel path: ", caffemodel_path print "Batch size: ", b_size print "Resize images: ", resize_im print "===================" print "----------------------" print "Preparing for Testing" print "======================" # Set GPU CPU setting if use_cpu: caffe.set_mode_cpu() else: # Use GPU caffe.set_device(gpu_dev) caffe.set_mode_gpu() print "Reading perspective file" if use_perspective: pers_file = h5py.File(perspective_path,'r') pmap = np.array( pers_file['pmap'] ) pers_file.close() mask = None if dataset == 'UCSD': print "Reading mask" if use_mask: mask_f = h5py.File(mask_file,'r') mask = np.array(mask_f['mask']) mask_f.close() print "Reading image file names:" im_names = np.loadtxt(test_names_file, dtype='str') # Perform test ntrueall=[] npredall=[] # Init GAME n_im = len( im_names ) game_table = np.zeros( (n_im, mx_game) ) # Init CNN CNN = CaffePredictor(prototxt_path, caffemodel_path, n_scales) print print "Start prediction ..." count = 0 gt_vector = np.zeros((len(im_names))) pred_vector = np.zeros((len(im_names))) for ix, name in enumerate(im_names): # Get image paths im_path = utl.extendName(name, im_folder) dot_im_path = utl.extendName(name, im_folder, use_ending=True, pattern=dot_ending) # Read image files im = loadImage(im_path, color = is_colored) dot_im = loadImage(dot_im_path, color = True) # Generate features if use_perspective: dens_im = genPDensity(dot_im, sigmadots, pmap) else: dens_im = genDensity(dot_im, sigmadots) if resize_im > 0: # Resize image im = utl.resizeMaxSize(im, resize_im) gt_sum = dens_im.sum() dens_im = utl.resizeMaxSize(dens_im, resize_im) dens_im = dens_im * gt_sum / dens_im.sum() # Get mask if needed if dataset != 'UCSD': if use_mask: mask_im_path = utl.extendName(name, im_folder, use_ending=True, pattern=mask_file) mask = sio.loadmat(mask_im_path, chars_as_strings=1, matlab_compatible=1) mask = mask.get('BW') s=time.time() ntrue,npred,resImg,gtdots=testOnImg(CNN, im, dens_im, pw, mask) print "image : %d , ntrue = %.2f ,npred = %.2f , time =%.2f sec"%(count,ntrue,npred,time.time()-s) # Keep individual predictions gt_vector[ix] = ntrue pred_vector[ix] = npred # Hold predictions and originasl ntrueall.append(ntrue) npredall.append(npred) # Compute game metric for l in range(mx_game): game_table[count, l] = gameMetric(resImg, gtdots, l) count = count +1 ntrueall=np.asarray(ntrueall) npredall=np.asarray(npredall) print "done ! mean absolute error %.2f" % np.mean(np.abs(ntrueall-npredall)) # Print Game results results = np.zeros(mx_game) for l in range(mx_game): results[l] = np.mean( game_table[:,l] ) print "GAME for level %d: %.2f " % (l, np.mean( game_table[:,l] )) # Dump results into a txt file np.savetxt(results_file + '_pred.txt', npredall) np.savetxt(results_file + '_gt.txt', ntrueall) return 0