def process(self): import gzip try: os.makedirs(os.path.join(self.root,self.raw_folder)) os.makedirs(os.path.join(self.root,self.processed_folder)) except: pass files=os.listdir(self.raw_folder) for file in files: print('Processing %s' % file) file_path=os.path.join(self.root,self.raw_folder,file) with open(file_path.replace('.gz',''),'wb') as out_f, \ gzip.GzipFile(file_path) as zip_f: out_f.write(zip_f.read()) training_set = ( read_image_file(os.path.join(self.root, self.raw_folder, 'train-images-idx3-ubyte')), read_label_file(os.path.join(self.root, self.raw_folder, 'train-labels-idx1-ubyte')) ) test_set = ( read_image_file(os.path.join(self.root, self.raw_folder, 't10k-images-idx3-ubyte')), read_label_file(os.path.join(self.root, self.raw_folder, 't10k-labels-idx1-ubyte')) ) with open(os.path.join(self.root, self.processed_folder, self.training_file), 'wb') as f: torch.save(training_set, f) with open(os.path.join(self.root, self.processed_folder, self.test_file), 'wb') as f: torch.save(test_set, f) print('Done!')
def gen(): engine = BasicEngine(model) labels = read_label_file(label) cap = get_cap() # a = engine.get_num_of_output_tensors() # b = engine.total_output_array_size() # c = engine.get_output_tensor_size(0) # d = engine.required_input_array_size() # print(a, b, c, d) while True: _, frame = cap.read() input_val = cv2.resize(frame, (432, 368)) input_val = input_val.flatten() ans = engine.RunInference(input_val) heat_map = ans[1].reshape([54, 46, 57]) prop = heat_map[1, :, :] prop = np.multiply(prop, 255) # prop = cv2.resize(prop, (460, 640)) _, buffer = cv2.imencode(".jpg", prop) yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + io.BytesIO(buffer).read() + b'\r\n')
def reorg_cifar10_data(data_dir, label_file, train_dir, test_dir, input_dir, valid_ratio): # Reorganize train & test data prevent overfit n_train_per_label, idx_label = utils.read_label_file( data_dir, label_file, train_dir, valid_ratio) utils.reorg_train_valid(data_dir, train_dir, input_dir, n_train_per_label, idx_label) utils.reorg_test(data_dir, test_dir, input_dir)
def _GetDataset(): _ITEMS_TO_DESCRIPTIONS = { 'image': 'A [W x H x Channel] image', 'label': 'A single integer' } keys_to_features = { 'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), 'image/format': tf.FixedLenFeature((), tf.string, default_value='jpg'), 'image/class/label': tf.FixedLenFeature([], tf.int64, default_value=tf.zeros([], dtype=tf.int64)), } shape = [cfg.IMAGE_SIZE, cfg.IMAGE_SIZE, cfg.IMAGE_CHANNEL] items_to_handlers = { 'image': slim.tfexample_decoder.Image(image_key='image/encoded', format_key='image/format', shape=shape, channels=cfg.IMAGE_CHANNEL), 'label': slim.tfexample_decoder.Tensor('image/class/label'), } decoder = slim.tfexample_decoder.TFExampleDecoder(keys_to_features, items_to_handlers) return slim.dataset.Dataset(data_sources=cfg.PATH_RECORD, reader=tf.TFRecordReader, decoder=decoder, num_samples=cfg.NUM_EXAMPLES, num_classes=cfg.NUM_CLASSES, items_to_descriptions=_ITEMS_TO_DESCRIPTIONS, labels_to_names=utils.read_label_file( cfg.PATH_LABELS))
def predict(image_fps, config, min_conf, submission_file=None, label_file=None, eval_dir=None): labels = None if label_file: labels = read_label_file(label_file) submit_dict = None if submission_file: submit_dict = {'patientId': [], 'PredictionString': []} for image_id in tqdm(image_fps): ds = pydicom.read_file(image_id) original_image = ds.pixel_array resize_factor = original_image.shape[0] / config.IMAGE_MIN_DIM # Convert to RGB for consistency. original_image = np.stack((original_image, ) * 3, -1) image, _, _, _, _ = utils.resize_image( original_image, min_dim=config.IMAGE_MIN_DIM, min_scale=config.IMAGE_MIN_SCALE, max_dim=config.IMAGE_MAX_DIM, mode=config.IMAGE_RESIZE_MODE) patient_id = os.path.splitext(os.path.basename(image_id))[0] results = model.detect([image]) r = results[0] assert (len(r['rois']) == len(r['class_ids']) == len(r['scores'])) if submit_dict: submit_dict['patientId'].append(patient_id) boxes = [] for i in range(len(r['rois'])): if r['scores'][i] > min_conf: x1 = r['rois'][i][1] y1 = r['rois'][i][0] width = (r['rois'][i][3] - x1) height = (r['rois'][i][2] - y1) boxes.append('{0} {1} {2} {3} {4}'.format( r['scores'][i], x1 * resize_factor, y1 * resize_factor, width * resize_factor, height * resize_factor)) if submit_dict: submit_dict['PredictionString'].append(' '.join(boxes)) # draw predicted boxes if len(r['rois']) != 0: visutil.visualize_boxes_and_labels_on_image_array( original_image, np.array(r['rois']) * resize_factor, r['class_ids'], r['scores'], {1: { 'id': 1, 'name': 'pneumonia' }}, use_normalized_coordinates=False, max_boxes_to_draw=3, min_score_thresh=min_conf, ) # draw ground truth boxes if labels: visutil.visualize_boxes_and_labels_on_image_array( original_image, labels[patient_id]['boxes'], [labels[patient_id]['label']] * len(labels[patient_id]['boxes']), None, {1: { 'id': 1, 'name': 'pneumonia' }}, use_normalized_coordinates=False, ) im = Image.fromarray(original_image) im.save(os.path.join(eval_dir, patient_id + '.jpg')) if submit_dict: pd.DataFrame(submit_dict).to_csv(submission_file, index=False)
parser.add_argument('weight_file') parser.add_argument('data_file') parser.add_argument('test_images_file') parser.add_argument('submission_file') parser.add_argument('images_out_dir') parser.add_argument('--label-file') parser.add_argument('-g', '--gpu-index', type=int, default=0) parser.add_argument('-t', '--threshold', type=float, default=0.2) args = parser.parse_args() if not os.path.exists(args.images_out_dir): os.mkdir(args.images_out_dir) labels = None if args.label_file: labels = read_label_file(args.label_file) net = load_net(args.config_file.encode(), args.weight_file.encode(), args.gpu_index) meta = load_meta(args.data_file.encode()) submit_dict = {'patientId': [], 'PredictionString': []} with open(args.test_images_file, 'r') as test_images_file: for line in tqdm(test_images_file): patient_id = os.path.split(line.strip())[1].split('.')[0] # read the image im = np.array(Image.open(line.strip())) infer_result = detect(net,