| @@ -0,0 +1,301 @@ | ||
| <<<<<<< HEAD | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| Created on Mon Oct 30 18:38:17 2017 | ||
| @author: 颜 | ||
| """ | ||
|
|
||
| import numpy as np | ||
| import os | ||
| import cv2 | ||
| import torch | ||
| from torch.autograd.variable import Variable | ||
| from config import Parameter | ||
|
|
||
|
|
||
| class Detector(object): | ||
| def __init__(self,para:Parameter): | ||
| self.para=para | ||
| self.yolo=torch.load(os.path.join(para.MODEL_PATH,para.LOAD_MODEL)) | ||
| self.yolo.para.batch_size=1 | ||
| self.offset=torch.Tensor(np.transpose(np.reshape(np.array([np.arange(para.S,dtype=float)]*para.S *para.B),(para.B,para.S,para.S)),(1,2,0))) | ||
| self.offset=Variable(self.offset) | ||
| if para.USING_GPU: | ||
| self.yolo=self.yolo.cuda() | ||
| self.offset=self.offset.cuda() | ||
| else: | ||
| self.yolo=self.yolo.cpu() | ||
| self.threshold=para.THRESHOLD | ||
| self.iou_threshold=para.IOU_THRESHOLD | ||
| self.bound_0=self.para.S*self.para.S*self.para.label_nums | ||
| self.bound_1=self.bound_0+self.para.S*self.para.S*self.para.B | ||
|
|
||
| def draw_result(self, img, result): | ||
| for i in range(len(result)): | ||
| x = int(result[i][1]) | ||
| y = int(result[i][2]) | ||
| w = int(result[i][3] / 2) | ||
| h = int(result[i][4] / 2) | ||
| cv2.rectangle(img, (x - w, y - h), (x + w, y + h), (0, 255, 0), 2) | ||
| cv2.rectangle(img, (x - w, y - h - 20), | ||
| (x + w, y - h), (125, 125, 125), -1) | ||
| cv2.putText(img, result[i][0] +' : %.2f' % result[i][5],(x-w+5,y-h-7), cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0),1,cv2.CV_16U) | ||
|
|
||
| def detect(self,image): | ||
| img_h,img_w,_=image.shape | ||
| inputs=cv2.resize(image,(448,448)) | ||
| inputs=cv2.cvtColor(inputs,cv2.COLOR_BGR2RGB).astype(np.float32) | ||
| inputs=(inputs/255.0)*2.0-1.0 | ||
| inputs=np.reshape(inputs,(1,448,448,3)) | ||
|
|
||
| result=self.detect_from_yolo(inputs)[0] | ||
|
|
||
| for i in range(len(result)): | ||
| result[i][1]*=(1.0*img_w/self.para.image_size) | ||
| result[i][2]*=(1.0*img_h/self.para.image_size) | ||
| result[i][3]*=(1.0*img_w/self.para.image_size) | ||
| result[i][4]*=(1.0*img_h/self.para.image_size) | ||
|
|
||
| return result | ||
|
|
||
| def detect_from_yolo(self, inputs): | ||
| inputs=Variable(torch.Tensor(inputs)) | ||
| if self.para.USING_GPU: | ||
| inputs=inputs.cuda() | ||
| output=self.yolo(inputs) | ||
| results=[] | ||
| for i in range(output.size()[0]): | ||
| results.append(self.interpret_output(output[i])) | ||
| return results | ||
|
|
||
| def interpret_output(self, predict): | ||
| predict_class_pro=predict[:self.bound_0].resize(self.para.S,self.para.S,self.para.label_nums) | ||
| predict_IOU=predict[self.bound_0:self.bound_1].resize(self.para.S,self.para.S,self.para.B) | ||
| predict_box_grid=predict[self.bound_1:].resize(self.para.S,self.para.S,self.para.B,4) | ||
| ''' | ||
| predict_box_grid=predict[:,:,self.para.B:self.para.B*5].cpu().numpy() | ||
| predict_box_grid=predict_box_grid.reshape(7,7,2,4) | ||
| predict_box_grid=torch.Tensor(predict_box_grid).cuda() | ||
| ''' | ||
| predict_box_image= torch.cat( | ||
| (((predict_box_grid[:,:,:,0]+self.offset)/float(self.para.S)*float(self.para.image_size)).resize(self.para.S,self.para.S,self.para.B,1), | ||
| ((predict_box_grid[:,:,:,1]+self.offset.transpose(0,1))/float(self.para.S)*float(self.para.image_size)).resize(self.para.S,self.para.S,self.para.B,1), | ||
| (torch.pow(predict_box_grid[:,:,:,2],2)*float(self.para.image_size)).resize(self.para.S,self.para.S,self.para.B,1), | ||
| (torch.pow(predict_box_grid[:,:,:,3],2)*float(self.para.image_size)).resize(self.para.S,self.para.S,self.para.B,1) | ||
| ),3) | ||
|
|
||
| prob=(predict_IOU.resize(self.para.S,self.para.S,self.para.B,1).expand(self.para.S,self.para.S,self.para.B,self.para.label_nums))*(predict_class_pro.resize(self.para.S,self.para.S,1,self.para.label_nums).expand(self.para.S,self.para.S,self.para.B,self.para.label_nums)) | ||
|
|
||
| ''' | ||
| prob = np.zeros((self.para.S,self.para.S,self.para.B,self.para.label_nums)) | ||
| for i in range(self.para.B): | ||
| for j in range(self.para.label_nums): | ||
| prob[:, :, i, j] = np.multiply(predict_class_pro.cpu().numpy()[:, :, j], predict_IOU.cpu().numpy()[:, :, i]) | ||
| prob=torch.Tensor(prob).cuda() | ||
| ''' | ||
| prob=prob.cpu().data.numpy() | ||
| predict_box_image=predict_box_image.cpu().data.numpy() | ||
|
|
||
| filter_mat_prob=np.array(prob>=self.threshold,dtype='bool')#shape=7x7x2x20 | ||
| filter_mat_boxe=np.nonzero(filter_mat_prob) | ||
| boxe_filtered=predict_box_image[filter_mat_boxe[0],filter_mat_boxe[1], filter_mat_boxe[2]]#shape=nx4 | ||
| prob_filtered=prob[filter_mat_prob]#size=n | ||
| classes_num_filtered=np.argmax(filter_mat_prob, axis=3)[filter_mat_boxe[0], filter_mat_boxe[1],filter_mat_boxe[2]]#size=n | ||
|
|
||
| argsort=np.array(np.argsort(prob_filtered))[::-1] | ||
| boxe_filtered=boxe_filtered[argsort]#size=nx4 | ||
| prob_filtered=prob_filtered[argsort]#size=n | ||
| classes_num_filtered = classes_num_filtered[argsort]#size=n | ||
|
|
||
| for i in range(len(boxe_filtered)): | ||
| if prob_filtered.data[i]==0: | ||
| continue | ||
| for j in range(i+1,len(boxe_filtered)): | ||
| if self.iou(boxe_filtered[i],boxe_filtered[j])>self.iou_threshold: | ||
| prob_filtered[j]=0.0 | ||
|
|
||
| filter_iou=np.array(prob_filtered > 0.0, dtype='bool') | ||
| boxe_filtered=boxe_filtered[filter_iou] | ||
| prob_filtered=prob_filtered[filter_iou] | ||
| classes_num_filtered=classes_num_filtered[filter_iou] | ||
|
|
||
| result=[] | ||
| for i in range(len(boxe_filtered)): | ||
| result.append([self.para.CLASSES[classes_num_filtered[i]], boxe_filtered[i][0], boxe_filtered[i][1], boxe_filtered[i][2], boxe_filtered[i][3], prob_filtered[i]]) | ||
|
|
||
| return result | ||
|
|
||
| def iou(self, box1, box2): | ||
| tb=min(box1[0]+0.5*box1[2],box2[0]+0.5*box2[2])-max(box1[0]-0.5*box1[2], box2[0]-0.5*box2[2]) | ||
| lr=min(box1[1]+0.5*box1[3],box2[1]+0.5*box2[3])-max(box1[1]-0.5*box1[3], box2[1]-0.5*box2[3]) | ||
| if tb < 0 or lr < 0: | ||
| intersection = 0 | ||
| else: | ||
| intersection=tb*lr | ||
| return intersection/(box1[2]*box1[3]+box2[2]*box2[3]-intersection) | ||
|
|
||
| def image_detector(self, image, wait=0): | ||
| #image=cv2.imread(imname) | ||
|
|
||
| result=self.detect(image) | ||
|
|
||
| self.draw_result(image, result) | ||
| image=cv2.resize(image,(600,600)) | ||
| return image | ||
| #cv2.imshow('Image', image) | ||
| #cv2.waitKey(wait) | ||
|
|
||
| para=Parameter | ||
| D=Detector(para) | ||
| ======= | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| Created on Mon Oct 30 18:38:17 2017 | ||
|
|
||
| @author: 颜 | ||
| """ | ||
|
|
||
| import numpy as np | ||
| import os | ||
| import cv2 | ||
| import torch | ||
| from torch.autograd.variable import Variable | ||
| from config import Parameter | ||
|
|
||
|
|
||
| class Detector(object): | ||
| def __init__(self,para:Parameter): | ||
| self.para=para | ||
| self.yolo=torch.load(os.path.join(para.MODEL_PATH,para.LOAD_MODEL)) | ||
| self.yolo.para.batch_size=1 | ||
| self.offset=torch.Tensor(np.transpose(np.reshape(np.array([np.arange(para.S,dtype=float)]*para.S *para.B),(para.B,para.S,para.S)),(1,2,0))) | ||
| self.offset=Variable(self.offset) | ||
| if para.USING_GPU: | ||
| self.yolo=self.yolo.cuda() | ||
| self.offset=self.offset.cuda() | ||
| else: | ||
| self.yolo=self.yolo.cpu() | ||
| self.threshold=para.THRESHOLD | ||
| self.iou_threshold=para.IOU_THRESHOLD | ||
| self.bound_0=self.para.S*self.para.S*self.para.label_nums | ||
| self.bound_1=self.bound_0+self.para.S*self.para.S*self.para.B | ||
|
|
||
| def draw_result(self, img, result): | ||
| for i in range(len(result)): | ||
| x = int(result[i][1]) | ||
| y = int(result[i][2]) | ||
| w = int(result[i][3] / 2) | ||
| h = int(result[i][4] / 2) | ||
| cv2.rectangle(img, (x - w, y - h), (x + w, y + h), (0, 255, 0), 2) | ||
| cv2.rectangle(img, (x - w, y - h - 20), | ||
| (x + w, y - h), (125, 125, 125), -1) | ||
| cv2.putText(img, result[i][0] +' : %.2f' % result[i][5],(x-w+5,y-h-7), cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0),1,cv2.CV_16U) | ||
|
|
||
| def detect(self,image): | ||
| img_h,img_w,_=image.shape | ||
| inputs=cv2.resize(image,(448,448)) | ||
| inputs=cv2.cvtColor(inputs,cv2.COLOR_BGR2RGB).astype(np.float32) | ||
| inputs=(inputs/255.0)*2.0-1.0 | ||
| inputs=np.reshape(inputs,(1,448,448,3)) | ||
|
|
||
| result=self.detect_from_yolo(inputs)[0] | ||
|
|
||
| for i in range(len(result)): | ||
| result[i][1]*=(1.0*img_w/self.para.image_size) | ||
| result[i][2]*=(1.0*img_h/self.para.image_size) | ||
| result[i][3]*=(1.0*img_w/self.para.image_size) | ||
| result[i][4]*=(1.0*img_h/self.para.image_size) | ||
|
|
||
| return result | ||
|
|
||
| def detect_from_yolo(self, inputs): | ||
| inputs=Variable(torch.Tensor(inputs)) | ||
| if self.para.USING_GPU: | ||
| inputs=inputs.cuda() | ||
| output=self.yolo(inputs) | ||
| results=[] | ||
| for i in range(output.size()[0]): | ||
| results.append(self.interpret_output(output[i])) | ||
| return results | ||
|
|
||
| def interpret_output(self, predict): | ||
| predict_class_pro=predict[:self.bound_0].resize(self.para.S,self.para.S,self.para.label_nums) | ||
| predict_IOU=predict[self.bound_0:self.bound_1].resize(self.para.S,self.para.S,self.para.B) | ||
| predict_box_grid=predict[self.bound_1:].resize(self.para.S,self.para.S,self.para.B,4) | ||
| ''' | ||
| predict_box_grid=predict[:,:,self.para.B:self.para.B*5].cpu().numpy() | ||
| predict_box_grid=predict_box_grid.reshape(7,7,2,4) | ||
| predict_box_grid=torch.Tensor(predict_box_grid).cuda() | ||
| ''' | ||
| predict_box_image= torch.cat( | ||
| (((predict_box_grid[:,:,:,0]+self.offset)/float(self.para.S)*float(self.para.image_size)).resize(self.para.S,self.para.S,self.para.B,1), | ||
| ((predict_box_grid[:,:,:,1]+self.offset.transpose(0,1))/float(self.para.S)*float(self.para.image_size)).resize(self.para.S,self.para.S,self.para.B,1), | ||
| (torch.pow(predict_box_grid[:,:,:,2],2)*float(self.para.image_size)).resize(self.para.S,self.para.S,self.para.B,1), | ||
| (torch.pow(predict_box_grid[:,:,:,3],2)*float(self.para.image_size)).resize(self.para.S,self.para.S,self.para.B,1) | ||
| ),3) | ||
|
|
||
| prob=(predict_IOU.resize(self.para.S,self.para.S,self.para.B,1).expand(self.para.S,self.para.S,self.para.B,self.para.label_nums))*(predict_class_pro.resize(self.para.S,self.para.S,1,self.para.label_nums).expand(self.para.S,self.para.S,self.para.B,self.para.label_nums)) | ||
|
|
||
| ''' | ||
| prob = np.zeros((self.para.S,self.para.S,self.para.B,self.para.label_nums)) | ||
| for i in range(self.para.B): | ||
| for j in range(self.para.label_nums): | ||
| prob[:, :, i, j] = np.multiply(predict_class_pro.cpu().numpy()[:, :, j], predict_IOU.cpu().numpy()[:, :, i]) | ||
| prob=torch.Tensor(prob).cuda() | ||
| ''' | ||
| prob=prob.cpu().data.numpy() | ||
| predict_box_image=predict_box_image.cpu().data.numpy() | ||
|
|
||
| filter_mat_prob=np.array(prob>=self.threshold,dtype='bool')#shape=7x7x2x20 | ||
| filter_mat_boxe=np.nonzero(filter_mat_prob) | ||
| boxe_filtered=predict_box_image[filter_mat_boxe[0],filter_mat_boxe[1], filter_mat_boxe[2]]#shape=nx4 | ||
| prob_filtered=prob[filter_mat_prob]#size=n | ||
| classes_num_filtered=np.argmax(filter_mat_prob, axis=3)[filter_mat_boxe[0], filter_mat_boxe[1],filter_mat_boxe[2]]#size=n | ||
|
|
||
| argsort=np.array(np.argsort(prob_filtered))[::-1] | ||
| boxe_filtered=boxe_filtered[argsort]#size=nx4 | ||
| prob_filtered=prob_filtered[argsort]#size=n | ||
| classes_num_filtered = classes_num_filtered[argsort]#size=n | ||
|
|
||
| for i in range(len(boxe_filtered)): | ||
| if prob_filtered.data[i]==0: | ||
| continue | ||
| for j in range(i+1,len(boxe_filtered)): | ||
| if self.iou(boxe_filtered[i],boxe_filtered[j])>self.iou_threshold: | ||
| prob_filtered[j]=0.0 | ||
|
|
||
| filter_iou=np.array(prob_filtered > 0.0, dtype='bool') | ||
| boxe_filtered=boxe_filtered[filter_iou] | ||
| prob_filtered=prob_filtered[filter_iou] | ||
| classes_num_filtered=classes_num_filtered[filter_iou] | ||
|
|
||
| result=[] | ||
| for i in range(len(boxe_filtered)): | ||
| result.append([self.para.CLASSES[classes_num_filtered[i]], boxe_filtered[i][0], boxe_filtered[i][1], boxe_filtered[i][2], boxe_filtered[i][3], prob_filtered[i]]) | ||
|
|
||
| return result | ||
|
|
||
| def iou(self, box1, box2): | ||
| tb=min(box1[0]+0.5*box1[2],box2[0]+0.5*box2[2])-max(box1[0]-0.5*box1[2], box2[0]-0.5*box2[2]) | ||
| lr=min(box1[1]+0.5*box1[3],box2[1]+0.5*box2[3])-max(box1[1]-0.5*box1[3], box2[1]-0.5*box2[3]) | ||
| if tb < 0 or lr < 0: | ||
| intersection = 0 | ||
| else: | ||
| intersection=tb*lr | ||
| return intersection/(box1[2]*box1[3]+box2[2]*box2[3]-intersection) | ||
|
|
||
| def image_detector(self, image, wait=0): | ||
| #image=cv2.imread(imname) | ||
|
|
||
| result=self.detect(image) | ||
|
|
||
| self.draw_result(image, result) | ||
| image=cv2.resize(image,(600,600)) | ||
| return image | ||
| #cv2.imshow('Image', image) | ||
| #cv2.waitKey(wait) | ||
|
|
||
| para=Parameter | ||
| D=Detector(para) | ||
| >>>>>>> 42493b708c3f0dcec3ead7688f05a4adf07b7508 |
| @@ -0,0 +1,282 @@ | ||
| <<<<<<< HEAD | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| Created on Thu Oct 26 12:21:41 2017 | ||
| @author: 颜 | ||
| """ | ||
|
|
||
| import os | ||
| import xml.etree.ElementTree as ET | ||
| import numpy as np | ||
| import cv2 | ||
| import pickle | ||
| import copy | ||
| import config | ||
| from tqdm import tqdm | ||
|
|
||
| class pascal_voc(): | ||
| def __init__(self,para): | ||
| self.devkil_path=os.path.join(para.VOC_PATH,'VOCdevkit') | ||
| self.data_path=os.path.join(self.devkil_path,para.VOC_VERSION) | ||
| self.cache_path=para.CACHE_PATH | ||
| self.batch_size=para.batch_size | ||
| self.image_size=para.image_size | ||
| self.cell_size=para.S | ||
| self.label_size=5+para.label_nums | ||
| self.classes=para.CLASSES | ||
| self.class_to_ind=dict(zip(self.classes, range(len(self.classes)))) | ||
| self.flipped=para.FLIPPED | ||
| self.rebuild=para.REBUILD | ||
| self.phase=para.PHASE | ||
| self.cursor=0 | ||
| self.epoch=1 | ||
| self.length=0 | ||
| self.gt_labels=None | ||
| self.prepare() | ||
|
|
||
| def generate_batch(self): | ||
| images=np.zeros((self.batch_size, self.image_size, self.image_size, 3)) | ||
| labels=np.zeros((self.batch_size, self.cell_size, self.cell_size, self.label_size)) | ||
| count = 0 | ||
| while count<self.batch_size: | ||
| imname=self.gt_labels[self.cursor]['imname'] | ||
| flipped=self.gt_labels[self.cursor]['flipped'] | ||
| images[count,:,:,:]=self.image_read(imname,flipped) | ||
| labels[count,:,:,:]=self.gt_labels[self.cursor]['label'] | ||
| count+=1 | ||
| self.cursor+=1 | ||
| if self.cursor>=self.length: | ||
| np.random.shuffle(self.gt_labels) | ||
| self.cursor=0 | ||
| self.epoch+=1 | ||
| return images,labels | ||
|
|
||
| def image_read(self, imname, flipped=False): | ||
| image = cv2.imread(imname) | ||
| image = cv2.resize(image,(self.image_size,self.image_size)) | ||
| image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32) | ||
| image = (image / 255.0) * 2.0 - 1.0 | ||
| if flipped: | ||
| image = image[:,::-1,:] | ||
| return image | ||
|
|
||
| def prepare(self): | ||
| gt_labels=self.load_labels() | ||
| if self.flipped: | ||
| print('Appending horizontally-flipped training examples ...') | ||
| gt_labels_cp = copy.deepcopy(gt_labels) | ||
| for idx in range(len(gt_labels_cp)): | ||
| gt_labels_cp[idx]['flipped']=True | ||
| gt_labels_cp[idx]['label']=gt_labels_cp[idx]['label'][:,::-1,:] | ||
| for i in range(self.cell_size): | ||
| for j in range(self.cell_size): | ||
| if gt_labels_cp[idx]['label'][i,j,0]==1: | ||
| gt_labels_cp[idx]['label'][i,j,1]=self.image_size-1-gt_labels_cp[idx]['label'][i,j,1] | ||
| gt_labels+=gt_labels_cp | ||
| np.random.shuffle(gt_labels) | ||
| self.gt_labels=gt_labels | ||
| self.length=len(gt_labels) | ||
| print(self.length,"images in total") | ||
| return gt_labels | ||
|
|
||
| def load_labels(self): | ||
| cache_file=os.path.join(self.cache_path,'pascal_'+self.phase+'_gt_labels.pkl') | ||
| if os.path.isfile(cache_file) and not self.rebuild: | ||
| print('Loading image_labels from:'+cache_file) | ||
| with open(cache_file, 'rb') as f: | ||
| gt_labels = pickle.load(f) | ||
| return gt_labels | ||
|
|
||
| print('Generating image_labels from:'+self.data_path) | ||
|
|
||
| if not os.path.exists(self.cache_path): | ||
| os.makedirs(self.cache_path) | ||
|
|
||
| if self.phase=='train': | ||
| txtname=os.path.join(self.data_path,'ImageSets','Main','trainval.txt') | ||
| else: | ||
| txtname=os.path.join(self.data_path,'ImageSets','Main','val.txt') | ||
| with open(txtname,'r') as f: | ||
| self.image_index=[x.strip() for x in f.readlines()] | ||
|
|
||
| gt_labels=[] | ||
| # for index in self.image_index: | ||
| # label,num=self.load_pascal_annotation(index) | ||
| for i in tqdm(range(len(self.image_index)),desc="Loading",ncols=110,ascii=True,unit="images"): | ||
| label,num=self.load_pascal_annotation(self.image_index[i]) | ||
| if num==0: | ||
| continue | ||
| imname=os.path.join(self.data_path,'JPEGImages',self.image_index[i]+'.jpg') | ||
| gt_labels.append({'imname':imname,'label':label,'flipped':False}) | ||
| print('Saving image_labels to: '+cache_file) | ||
| with open(cache_file,'wb') as f: | ||
| pickle.dump(gt_labels,f) | ||
| return gt_labels | ||
|
|
||
| def load_pascal_annotation(self, index): | ||
| imname=os.path.join(self.data_path,'JPEGImages',index +'.jpg') | ||
| im=cv2.imread(imname) | ||
| h_ratio=1.0*self.image_size/im.shape[0] | ||
| w_ratio=1.0*self.image_size/im.shape[1] | ||
| label=np.zeros((self.cell_size,self.cell_size,self.label_size)) | ||
| filename=os.path.join(self.data_path,'Annotations',index +'.xml') | ||
| tree=ET.parse(filename) | ||
| objs=tree.findall('object') | ||
| for obj in objs: | ||
| bbox=obj.find('bndbox') | ||
| x1=max(min((float(bbox.find('xmin').text)-1)*w_ratio,self.image_size-1),0) | ||
| y1=max(min((float(bbox.find('ymin').text)-1)*h_ratio,self.image_size-1),0) | ||
| x2=max(min((float(bbox.find('xmax').text)-1)*w_ratio,self.image_size-1),0) | ||
| y2=max(min((float(bbox.find('ymax').text)-1)*h_ratio,self.image_size-1),0) | ||
| cls_ind=self.class_to_ind[obj.find('name').text.lower().strip()] | ||
| boxes=[(x2+x1)/2.0,(y2+y1)/2.0,x2-x1,y2-y1] | ||
| x_ind=int(boxes[0]*self.cell_size/self.image_size) | ||
| y_ind=int(boxes[1]*self.cell_size/self.image_size) | ||
| if label[y_ind,x_ind,0]==1: | ||
| continue | ||
| label[y_ind,x_ind,0]=1 | ||
| label[y_ind,x_ind,1:5]=boxes | ||
| label[y_ind,x_ind,5+cls_ind]=1 | ||
| ======= | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| Created on Thu Oct 26 12:21:41 2017 | ||
|
|
||
| @author: 颜 | ||
| """ | ||
|
|
||
| import os | ||
| import xml.etree.ElementTree as ET | ||
| import numpy as np | ||
| import cv2 | ||
| import pickle | ||
| import copy | ||
| import config | ||
| from tqdm import tqdm | ||
|
|
||
| class pascal_voc(): | ||
| def __init__(self,para): | ||
| self.devkil_path=os.path.join(para.VOC_PATH,'VOCdevkit') | ||
| self.data_path=os.path.join(self.devkil_path,para.VOC_VERSION) | ||
| self.cache_path=para.CACHE_PATH | ||
| self.batch_size=para.batch_size | ||
| self.image_size=para.image_size | ||
| self.cell_size=para.S | ||
| self.label_size=5+para.label_nums | ||
| self.classes=para.CLASSES | ||
| self.class_to_ind=dict(zip(self.classes, range(len(self.classes)))) | ||
| self.flipped=para.FLIPPED | ||
| self.rebuild=para.REBUILD | ||
| self.phase=para.PHASE | ||
| self.cursor=0 | ||
| self.epoch=1 | ||
| self.length=0 | ||
| self.gt_labels=None | ||
| self.prepare() | ||
|
|
||
| def generate_batch(self): | ||
| images=np.zeros((self.batch_size, self.image_size, self.image_size, 3)) | ||
| labels=np.zeros((self.batch_size, self.cell_size, self.cell_size, self.label_size)) | ||
| count = 0 | ||
| while count<self.batch_size: | ||
| imname=self.gt_labels[self.cursor]['imname'] | ||
| flipped=self.gt_labels[self.cursor]['flipped'] | ||
| images[count,:,:,:]=self.image_read(imname,flipped) | ||
| labels[count,:,:,:]=self.gt_labels[self.cursor]['label'] | ||
| count+=1 | ||
| self.cursor+=1 | ||
| if self.cursor>=self.length: | ||
| np.random.shuffle(self.gt_labels) | ||
| self.cursor=0 | ||
| self.epoch+=1 | ||
| return images,labels | ||
|
|
||
| def image_read(self, imname, flipped=False): | ||
| image = cv2.imread(imname) | ||
| image = cv2.resize(image,(self.image_size,self.image_size)) | ||
| image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32) | ||
| image = (image / 255.0) * 2.0 - 1.0 | ||
| if flipped: | ||
| image = image[:,::-1,:] | ||
| return image | ||
|
|
||
| def prepare(self): | ||
| gt_labels=self.load_labels() | ||
| if self.flipped: | ||
| print('Appending horizontally-flipped training examples ...') | ||
| gt_labels_cp = copy.deepcopy(gt_labels) | ||
| for idx in range(len(gt_labels_cp)): | ||
| gt_labels_cp[idx]['flipped']=True | ||
| gt_labels_cp[idx]['label']=gt_labels_cp[idx]['label'][:,::-1,:] | ||
| for i in range(self.cell_size): | ||
| for j in range(self.cell_size): | ||
| if gt_labels_cp[idx]['label'][i,j,0]==1: | ||
| gt_labels_cp[idx]['label'][i,j,1]=self.image_size-1-gt_labels_cp[idx]['label'][i,j,1] | ||
| gt_labels+=gt_labels_cp | ||
| np.random.shuffle(gt_labels) | ||
| self.gt_labels=gt_labels | ||
| self.length=len(gt_labels) | ||
| print(self.length,"images in total") | ||
| return gt_labels | ||
|
|
||
| def load_labels(self): | ||
| cache_file=os.path.join(self.cache_path,'pascal_'+self.phase+'_gt_labels.pkl') | ||
| if os.path.isfile(cache_file) and not self.rebuild: | ||
| print('Loading image_labels from:'+cache_file) | ||
| with open(cache_file, 'rb') as f: | ||
| gt_labels = pickle.load(f) | ||
| return gt_labels | ||
|
|
||
| print('Generating image_labels from:'+self.data_path) | ||
|
|
||
| if not os.path.exists(self.cache_path): | ||
| os.makedirs(self.cache_path) | ||
|
|
||
| if self.phase=='train': | ||
| txtname=os.path.join(self.data_path,'ImageSets','Main','trainval.txt') | ||
| else: | ||
| txtname=os.path.join(self.data_path,'ImageSets','Main','val.txt') | ||
| with open(txtname,'r') as f: | ||
| self.image_index=[x.strip() for x in f.readlines()] | ||
|
|
||
| gt_labels=[] | ||
| # for index in self.image_index: | ||
| # label,num=self.load_pascal_annotation(index) | ||
| for i in tqdm(range(len(self.image_index)),desc="Loading",ncols=110,ascii=True,unit="images"): | ||
| label,num=self.load_pascal_annotation(self.image_index[i]) | ||
| if num==0: | ||
| continue | ||
| imname=os.path.join(self.data_path,'JPEGImages',self.image_index[i]+'.jpg') | ||
| gt_labels.append({'imname':imname,'label':label,'flipped':False}) | ||
| print('Saving image_labels to: '+cache_file) | ||
| with open(cache_file,'wb') as f: | ||
| pickle.dump(gt_labels,f) | ||
| return gt_labels | ||
|
|
||
| def load_pascal_annotation(self, index): | ||
| imname=os.path.join(self.data_path,'JPEGImages',index +'.jpg') | ||
| im=cv2.imread(imname) | ||
| h_ratio=1.0*self.image_size/im.shape[0] | ||
| w_ratio=1.0*self.image_size/im.shape[1] | ||
| label=np.zeros((self.cell_size,self.cell_size,self.label_size)) | ||
| filename=os.path.join(self.data_path,'Annotations',index +'.xml') | ||
| tree=ET.parse(filename) | ||
| objs=tree.findall('object') | ||
| for obj in objs: | ||
| bbox=obj.find('bndbox') | ||
| x1=max(min((float(bbox.find('xmin').text)-1)*w_ratio,self.image_size-1),0) | ||
| y1=max(min((float(bbox.find('ymin').text)-1)*h_ratio,self.image_size-1),0) | ||
| x2=max(min((float(bbox.find('xmax').text)-1)*w_ratio,self.image_size-1),0) | ||
| y2=max(min((float(bbox.find('ymax').text)-1)*h_ratio,self.image_size-1),0) | ||
| cls_ind=self.class_to_ind[obj.find('name').text.lower().strip()] | ||
| boxes=[(x2+x1)/2.0,(y2+y1)/2.0,x2-x1,y2-y1] | ||
| x_ind=int(boxes[0]*self.cell_size/self.image_size) | ||
| y_ind=int(boxes[1]*self.cell_size/self.image_size) | ||
| if label[y_ind,x_ind,0]==1: | ||
| continue | ||
| label[y_ind,x_ind,0]=1 | ||
| label[y_ind,x_ind,1:5]=boxes | ||
| label[y_ind,x_ind,5+cls_ind]=1 | ||
| >>>>>>> 42493b708c3f0dcec3ead7688f05a4adf07b7508 | ||
| return label,len(objs) |
| @@ -0,0 +1,132 @@ | ||
| <<<<<<< HEAD | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| Created on Tue Oct 24 18:23:38 2017 | ||
| @author: 颜 | ||
| """ | ||
|
|
||
| import torch | ||
| import config | ||
| from torch.autograd.variable import Variable | ||
| from tqdm import tqdm | ||
| #from config import init_weight | ||
| from yolo_net import YOLO | ||
| from voc import pascal_voc | ||
| import os | ||
| import cv2 | ||
|
|
||
| if __name__=='__main__': | ||
| __spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)" | ||
| para=config.Parameter | ||
| voc=pascal_voc(para) | ||
| load_model=os.path.join(para.MODEL_PATH,para.LOAD_MODEL) | ||
| save_model=os.path.join(para.MODEL_PATH,para.SAVE_MODEL) | ||
| if os.path.isfile(load_model): | ||
| yolo=torch.load(load_model) | ||
| else: | ||
| yolo=YOLO(para) | ||
| if para.USING_PRETRAINED_WEIGHT: | ||
| yolo.load_weight() | ||
| if para.USING_GPU: | ||
| yolo.cuda() | ||
| #yolo.apply(init_weight) | ||
| optimizer=torch.optim.SGD(yolo.parameters(),lr=0.0001,momentum=0.9,weight_decay=0.0005) | ||
| LOSS=0 | ||
| t=tqdm(range(para.train_step),desc="Trainer",ncols=120,ascii=True,unit="Batches") | ||
| info={'INIT_LOSS':0.,'LOSS':0.,'epoch':0.} | ||
| for i in t: | ||
| yolo.zero_grad() | ||
| images,labels=voc.generate_batch() | ||
| images=Variable(torch.Tensor(images)) | ||
| labels=Variable(torch.Tensor(labels)) | ||
| if para.USING_GPU: | ||
| images=images.cuda() | ||
| labels=labels.cuda() | ||
| predict=yolo.forward(images) | ||
| loss=yolo.loss(predict,labels) | ||
| loss.backward() | ||
| LOSS+=loss.cpu().data[0] | ||
| optimizer.step() | ||
| if i==0: | ||
| info['INIT_LOSS']=LOSS | ||
| t.set_postfix(**info) | ||
| elif i%para.check_point==0: | ||
| info['LOSS']=LOSS/para.check_point | ||
| info['epoch']=voc.epoch | ||
| LOSS=0 | ||
| t.set_postfix(**info) | ||
| if i%para.save_point==0 and not i==0: | ||
| torch.save(yolo,save_model) | ||
| torch.save(yolo,save_model) | ||
| os.system("pause") | ||
| ''' | ||
| for para in yolo.parameters(): | ||
| print(para.data.size()) | ||
| ======= | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| Created on Tue Oct 24 18:23:38 2017 | ||
| @author: 颜 | ||
| """ | ||
| import torch | ||
| import config | ||
| from torch.autograd.variable import Variable | ||
| from tqdm import tqdm | ||
| #from config import init_weight | ||
| from yolo_net import YOLO | ||
| from voc import pascal_voc | ||
| import os | ||
| import cv2 | ||
| if __name__=='__main__': | ||
| __spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)" | ||
| para=config.Parameter | ||
| voc=pascal_voc(para) | ||
| load_model=os.path.join(para.MODEL_PATH,para.LOAD_MODEL) | ||
| save_model=os.path.join(para.MODEL_PATH,para.SAVE_MODEL) | ||
| if os.path.isfile(load_model): | ||
| yolo=torch.load(load_model) | ||
| else: | ||
| yolo=YOLO(para) | ||
| if para.USING_PRETRAINED_WEIGHT: | ||
| yolo.load_weight() | ||
| if para.USING_GPU: | ||
| yolo.cuda() | ||
| #yolo.apply(init_weight) | ||
| optimizer=torch.optim.SGD(yolo.parameters(),lr=0.0001,momentum=0.9,weight_decay=0.0005) | ||
| LOSS=0 | ||
| t=tqdm(range(para.train_step),desc="Trainer",ncols=120,ascii=True,unit="Batches") | ||
| info={'INIT_LOSS':0.,'LOSS':0.,'epoch':0.} | ||
| for i in t: | ||
| yolo.zero_grad() | ||
| images,labels=voc.generate_batch() | ||
| images=Variable(torch.Tensor(images)) | ||
| labels=Variable(torch.Tensor(labels)) | ||
| if para.USING_GPU: | ||
| images=images.cuda() | ||
| labels=labels.cuda() | ||
| predict=yolo.forward(images) | ||
| loss=yolo.loss(predict,labels) | ||
| loss.backward() | ||
| LOSS+=loss.cpu().data[0] | ||
| optimizer.step() | ||
| if i==0: | ||
| info['INIT_LOSS']=LOSS | ||
| t.set_postfix(**info) | ||
| elif i%para.check_point==0: | ||
| info['LOSS']=LOSS/para.check_point | ||
| info['epoch']=voc.epoch | ||
| LOSS=0 | ||
| t.set_postfix(**info) | ||
| if i%para.save_point==0 and not i==0: | ||
| torch.save(yolo,save_model) | ||
| torch.save(yolo,save_model) | ||
| os.system("pause") | ||
| ''' | ||
| for para in yolo.parameters(): | ||
| print(para.data.size()) | ||
| >>>>>>> 42493b708c3f0dcec3ead7688f05a4adf07b7508 | ||
| ''' |
| @@ -0,0 +1,134 @@ | ||
| <<<<<<< HEAD | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| Created on Tue Oct 24 18:23:38 2017 | ||
| @author: 颜 | ||
| """ | ||
|
|
||
| import torch | ||
| import config | ||
| from torch.autograd.variable import Variable | ||
| from tqdm import tqdm | ||
| #from config import init_weight | ||
| from yolo_net import YOLO | ||
| from voc import pascal_voc | ||
| import torch.multiprocessing as mp | ||
| import os | ||
|
|
||
|
|
||
| def train(yolo,para,voc,rank): | ||
| optimizer=torch.optim.SGD(yolo.parameters(),lr=0.01,momentum=0.9,weight_decay=0.0005) | ||
| LOSS=0 | ||
| t=tqdm(range(para.train_step),desc="Trainer"+str(rank),ncols=110,ascii=True,unit="Batches",position=rank) | ||
| info={'INIT_LOSS':0.,'LOSS':0.,'epoch':0.} | ||
| for i in t: | ||
| if (voc.cursor-rank)%para.CPU_PROCESSING==0: | ||
| yolo.zero_grad() | ||
| images,labels=voc.generate_batch() | ||
| images=Variable(torch.Tensor(images)) | ||
| labels=Variable(torch.Tensor(labels)) | ||
| if para.USING_GPU: | ||
| images=images.cuda() | ||
| labels=labels.cuda() | ||
| predict=yolo.forward(images) | ||
| loss=yolo.loss(predict,labels) | ||
| loss.backward() | ||
| LOSS+=loss.cpu().data[0] | ||
| optimizer.step() | ||
| if i==rank: | ||
| info['INIT_LOSS']=LOSS | ||
| t.set_postfix(**info) | ||
| elif i%para.check_point==0: | ||
| info['LOSS']=LOSS/para.check_point | ||
| info['epoch']=voc.epoch | ||
| LOSS=0 | ||
| t.set_postfix(**info) | ||
| if i%para.save_point==0 and not i==0: | ||
| torch.save(yolo,para.MODULE_NAME) | ||
| else: | ||
| voc.cursor+=1 | ||
|
|
||
| if __name__=='__main__': | ||
| __spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)" | ||
| para=config.Parameter | ||
| voc=pascal_voc(para) | ||
| yolo=YOLO(para) | ||
| if para.USING_GPU: | ||
| yolo.cuda() | ||
| #yolo.apply(init_weight) | ||
| processes = [] | ||
| for rank in range(para.CPU_PROCESSING): | ||
| p = mp.Process(target=train, args=(yolo,para,voc,rank)) | ||
| p.start() | ||
| processes.append(p) | ||
| for p in processes: | ||
| p.join() | ||
| ======= | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| Created on Tue Oct 24 18:23:38 2017 | ||
|
|
||
| @author: 颜 | ||
| """ | ||
|
|
||
| import torch | ||
| import config | ||
| from torch.autograd.variable import Variable | ||
| from tqdm import tqdm | ||
| #from config import init_weight | ||
| from yolo_net import YOLO | ||
| from voc import pascal_voc | ||
| import torch.multiprocessing as mp | ||
| import os | ||
|
|
||
|
|
||
| def train(yolo,para,voc,rank): | ||
| optimizer=torch.optim.SGD(yolo.parameters(),lr=0.01,momentum=0.9,weight_decay=0.0005) | ||
| LOSS=0 | ||
| t=tqdm(range(para.train_step),desc="Trainer"+str(rank),ncols=110,ascii=True,unit="Batches",position=rank) | ||
| info={'INIT_LOSS':0.,'LOSS':0.,'epoch':0.} | ||
| for i in t: | ||
| if (voc.cursor-rank)%para.CPU_PROCESSING==0: | ||
| yolo.zero_grad() | ||
| images,labels=voc.generate_batch() | ||
| images=Variable(torch.Tensor(images)) | ||
| labels=Variable(torch.Tensor(labels)) | ||
| if para.USING_GPU: | ||
| images=images.cuda() | ||
| labels=labels.cuda() | ||
| predict=yolo.forward(images) | ||
| loss=yolo.loss(predict,labels) | ||
| loss.backward() | ||
| LOSS+=loss.cpu().data[0] | ||
| optimizer.step() | ||
| if i==rank: | ||
| info['INIT_LOSS']=LOSS | ||
| t.set_postfix(**info) | ||
| elif i%para.check_point==0: | ||
| info['LOSS']=LOSS/para.check_point | ||
| info['epoch']=voc.epoch | ||
| LOSS=0 | ||
| t.set_postfix(**info) | ||
| if i%para.save_point==0 and not i==0: | ||
| torch.save(yolo,para.MODULE_NAME) | ||
| else: | ||
| voc.cursor+=1 | ||
|
|
||
| if __name__=='__main__': | ||
| __spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)" | ||
| para=config.Parameter | ||
| voc=pascal_voc(para) | ||
| yolo=YOLO(para) | ||
| if para.USING_GPU: | ||
| yolo.cuda() | ||
| #yolo.apply(init_weight) | ||
| processes = [] | ||
| for rank in range(para.CPU_PROCESSING): | ||
| p = mp.Process(target=train, args=(yolo,para,voc,rank)) | ||
| p.start() | ||
| processes.append(p) | ||
| for p in processes: | ||
| p.join() | ||
| >>>>>>> 42493b708c3f0dcec3ead7688f05a4adf07b7508 | ||
| os.system("pause") |