def __init__(self,data_file): print('Reading data...') self.fnames = [] f = open(data_file) self.data = [] cnt = 0 for i in f: i = i.strip().split('\t') jpgfile = i[0] # get jpg, seg, mask jpg = img_reader.read_img(jpgfile,500,padding=True) seg = data_provider.get_seg(jpgfile) seg = img_reader.pad(seg,np.uint8,False) mask = seg.copy() seg[seg==255] = 0 mask[mask!=0] = 1 coords = data_provider.get_coord_map(jpgfile) coords = img_reader.pad(coords,np.float32,6) inst_num = data_provider.get_inst_num(jpgfile) if seg.shape[0]!=500: print(jpgfile) continue # if size is ok, add to data list self.data.append([jpg,seg,mask,coords,inst_num]) self.fnames.append(jpgfile) cnt += 1 if cnt%100==0: print(cnt)
def __init__(self, data_file): print('Reading data...') self.fnames = [] f = open(data_file) self.data = [] cnt = 0 for i in f: i = i.strip() jpgfile = './data/reform_img/' + i segfile = './data/reform_annot/' + i.replace('.jpg', '.png') jpg = img_reader.read_img(jpgfile, 500, padding=True) seg = self.read_label(segfile) seg = img_reader.pad(seg, np.uint8, False) mask = seg.copy() seg[seg == 255] = 0 mask[mask != 0] = 1 if seg.shape[0] != 500: print(segfile) continue self.data.append([jpg, seg, mask]) self.fnames.append(jpgfile) cnt += 1 if cnt % 100 == 0: print(cnt) print('Data length:', len(self.data))
def __init__(self, data_file): print('Reading data...') self.fnames = [] f = open(data_file) self.data = [] cnt = 0 for i in f: i = i.strip() segfile = i jpgfile = i.replace('SegmentationClass', 'JPEGImages').replace('.png', '.jpg') jpg = img_reader.read_img(jpgfile, 500, padding=True) seg = self.read_label(segfile) seg = img_reader.pad(seg, np.uint8, False) seg[seg != 15] = 0 seg[seg == 15] = 1 if seg.shape[0] != 500: print(segfile) continue self.data.append([jpg, seg]) self.fnames.append(jpgfile) cnt += 1 if cnt % 100 == 0: print(cnt) print('Data length:', len(self.data))
def __init__(self,data_file): print('Reading data...') self.fnames = [] f = open(data_file) self.data = [] cnt = 0 for i in f: try: i = i.strip() segfile = i.split('\t')[1] jpgfile = i.split('\t')[0] jpg = img_reader.read_img(jpgfile,500,padding=True) seg = self.read_label(segfile) seg = img_reader.pad(seg,np.uint8,False) seg[seg!=15] = 0 seg[seg==15] = 1 if seg.shape[0]!=500: print(segfile) continue self.data.append([jpg,seg]) self.fnames.append(jpgfile) cnt += 1 if cnt%100==0: print(cnt) except: pass print('Data length:',len(self.data))
reader = data_provider('train_list.txt') with tf.variable_scope('seg_part'): net = network(21) MAX_ITER = 100000 BSIZE = 1 for i in range(MAX_ITER): img_batch, lab_batch, mask_batch = reader.next_batch(BSIZE) loss = net.train(img_batch, lab_batch, mask_batch) print('Iter:%d\tLoss:%.4f' % (i, loss)) # monitor the training process if i % 10 == 0: fname = random.sample(reader.fnames, 1) img = cv2.imread(fname[0]) img = img_reader.pad(img, np.uint8) img_inp = img_reader.read_img(fname[0], 500, True) mask = read_label(fname[0].replace('reform_img', 'reform_annot').replace( 'jpg', 'png')) mask = img_reader.pad(mask, np.uint8, False) mask[mask != 0] = 1 img = img[20:480, 20:480] img_inp = img_inp[20:480, 20:480] mask = mask[20:480, 20:480] res = net.eval([img_inp], [mask]) res = np.uint8(res) res = cv2.resize(res, (img.shape[1], img.shape[0]),
with tf.variable_scope('bg_fg'): net = network() MAX_ITER = 100000 BSIZE = 1 for i in range(MAX_ITER): img_batch, lab_batch = reader.next_batch(BSIZE) loss = net.train(img_batch, lab_batch) print('Iter:%d\tLoss:%.4f' % (i, loss)) # monitor the training process if i % 10 == 0: fname = random.sample(reader.fnames, 1) img = cv2.imread(fname[0]) img = img_reader.pad(img, np.uint8) img_inp = img_reader.read_img(fname[0], 500, True) img = img[20:480, 20:480] img_inp = img_inp[20:480, 20:480] res = net.eval([img_inp]) res = np.uint8(res) res = cv2.resize(res, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_NEAREST) res[res == 1] = 255 cv2.imwrite('./sample_bgfg/%d.jpg' % (i), img) cv2.imwrite('./sample_bgfg/%d_2.jpg' % (i), res) # save model if i % 1000 == 0 and i > 0: