def pickle2npz(): nn12 = Cnnl('12-net').__load_model_old__('12-net_lasagne_.pickle') nn12.save_model('12-net_lasagne_.npz') nn24 = Cnnl(nn_name='24-net', subnet=nn12).__load_model_old__('24-net_lasagne_.pickle') nn48 = Cnnl(nn_name='48-net', subnet=nn24).__load_model_old__('48-net_lasagne_.pickle') nn24.save_model('24-net_lasagne_.npz') nn48.save_model('48-net_lasagne_.npz')
def train(nn_name='12-net', k=12): """ Fucntion for traning 12-net with testing on part of data using cross validation """ suff = str(k) if nn_name.find('calib') > 0: X_data_name = 'train_data_icalib_' + suff + '.npy' y_data_name = 'labels_icalib_' + suff + '.npy' else: X_data_name = 'train_data_' + suff + '.npy' y_data_name = 'labels_' + suff + '.npy' rates12 = sp.hstack((0.05 * sp.ones(25, dtype=sp.float32), 0.005 * sp.ones(15, dtype=sp.float32), 0.0005 * sp.ones(10, dtype=sp.float32))) rates24 = sp.hstack((0.01 * sp.ones(25, dtype=sp.float32), 0.0001 * sp.ones(15, dtype=sp.float32))) rates48 = sp.hstack([ 0.05 * sp.ones(15, dtype=sp.float32), 0.005 * sp.ones(10, dtype=sp.float32) ]) if nn_name == '24-net': nn = Cnnl( nn_name=nn_name, l_rates=rates24, subnet=Cnnl(nn_name='12-net', l_rates=rates12).load_model('12-net_lasagne_.pickle')) elif nn_name == '48-net': nn = Cnnl(nn_name=nn_name, l_rates=rates48, subnet=Cnnl( nn_name='24-net', l_rates=rates24, subnet=Cnnl( nn_name='12-net', l_rates=rates12).load_model('12-net_lasagne_.pickle') ).load_model('24-net_lasagne_.pickle')) else: nn = Cnnl(nn_name=nn_name, l_rates=rates12) if not os.path.exists(nn_name + '_lasagne_.pickle'): if nn_name.find('calib') > 0: ds.get_train_wider_calib_data(k=k) else: ds.get_train_data(k=k) X, y = sp.load(X_data_name), sp.load(y_data_name) X_train, y_train = X, y if not os.path.exists(nn_name + '_lasagne_.pickle'): if nn_name == '24-net': X_sub_train12 = sp.load('train_data_12.npy') nn.fit(X=X_train, y=y_train, X12=X_sub_train12) elif nn_name == '48-net': X_sub_train12 = sp.load('train_data_12.npy') X_sub_train24 = sp.load('train_data_24.npy') nn.fit(X=X_train, y=y_train, X12=X_sub_train12, X24=X_sub_train24) else: nn.fit(X=X_train, y=y_train) nn.save_model(nn_name + '_lasagne_.pickle')
def main(): iname = 'img/test/1226.jpg' nn12 = Cnnl('12-net').load_model('12-net_lasagne_.pickle') nn_calib12 = Cnnl('12-calib_net').load_model( '12-calib_net_lasagne_.pickle') nn24 = Cnnl(nn_name='24-net', subnet=nn12).load_model('24-net_lasagne_.pickle') nn_calib24 = Cnnl('24-calib_net').load_model( '24-calib_net_lasagne_.pickle') nn48 = Cnnl(nn_name='48-net', subnet=nn24).load_model('48-net_lasagne_.pickle') nn_calib48 = Cnnl('48-calib_net').load_model( '48-calib_net_lasagne_.pickle') image = fr.get_frame(iname) p = 16 k = 1.18 bb = apply_12_net(image=image, iname=iname, nn=nn12, p_level=p, k=k, debug=0) bb = apply_calib_net(image=image, iname=iname, bb=bb, nn=nn_calib12) bb = util.nms(bb, T=0.8) bb = apply_24_net(image=image, iname=iname, bb=bb, nn=nn24) bb = apply_calib_net(image=image, iname=iname, bb=bb, nn=nn_calib24) bb = util.nms(bb, T=0.8) bb = apply_48_net(image=image, iname=iname, bb=bb, nn=nn48) bb = util.nms(bb, T=0.3) bb = apply_calib_net(image=image, iname=iname, bb=bb, nn=nn_calib48) draw_bboxes(iname, bb)
def train(nn_name='12-net', rootdir='F:\\'): """ Fucntion for traning 12-net with testing on part of data using cross validation """ is_calib = nn_name.find('calib') > 0 X_name = 'train_data_' + ('icalib_' if is_calib else '') + nn_name[:2] + '.npz' y_name = 'labels_' + ('icalib_' if is_calib else '') + nn_name[:2] + '.npz' is_calib = nn_name.find('calib') > 0 rates = sp.hstack((0.05 * sp.ones(333, dtype=sp.float32), 0.005 * sp.ones(333, dtype=sp.float32), 0.0005 * sp.ones(333, dtype=sp.float32), 0.00005 * sp.ones(5, dtype=sp.float32))) rates12 = rates rates24 = rates rates48 = rates rates12_c = rates12[333:666] rates48_c = rates12_c rates24_c = rates12_c if nn_name == '24-net': if is_calib: nn = Cnnl(nn_name=nn_name, l_rates=rates24_c) else: nn = Cnnl(nn_name=nn_name, l_rates=rates24) elif nn_name == '48-net': if is_calib: nn = Cnnl(nn_name=nn_name, l_rates=rates48_c) else: nn = Cnnl(nn_name=nn_name, l_rates=rates48) else: if is_calib: nn = Cnnl(nn_name=nn_name, l_rates=rates12_c) else: nn = Cnnl(nn_name=nn_name, l_rates=rates12) if is_calib: X = util.load_from_npz(X_name) y = util.load_from_npz(y_name) nn.fit(X, y) else: rpath = os.path.join(rootdir, 'traindata', nn.nn_name) nn.train_on_hdd(rootdir=rpath) nn.__save_model_old__(nn_name + '_lasagne_.pickle')
def main(): iname = 'img/test/p1.jpg' nn12 = Cnnl('12-net').__load_model_old__('12-net_lasagne_.pickle') nn24 = Cnnl(nn_name = '24-net',subnet=nn12).__load_model_old__('24-net_lasagne_.pickle') nn48 = Cnnl(nn_name = '48-net',subnet=nn24).__load_model_old__('48-net_lasagne_.pickle') nn_calib12 = Cnnl('12-calib_net').__load_model_old__('12-calib_net_lasagne_.pickle') nn_calib24 = Cnnl('24-calib_net').__load_model_old__('24-calib_net_lasagne_.pickle') #nn_calib48 = Cnnl('48-calib_net').__load_model_old__('48-calib_net_lasagne_.pickle') image = fr.get_frame(iname) p = 11 k = 1.41 t_level = 0 gbb = [] gp = [] print('Start....') s = time() py = fr.get_frame_pyramid(image,scale=k,steps=p) for m,frame in enumerate(py): if m < t_level: continue bb,p = apply_12_net(image = image,frame=frame,iname = iname,nn = nn12,m=m,debug=0) keep = util.nms(bb,p,T=0.5) bb = bb[keep] if keep != [] else bb p = p[keep] if keep != [] else p cnt_detect = len(bb) if cnt_detect == 0: continue #draw_bboxes(iname,bb,c='#-r_12--' + str(m)) for b in bb: gbb.append(b) for pp in p: gp.append(pp) gbb = sp.array(gbb) gp = sp.array(gp) #draw_bboxes(iname,gbb,c='0-r_12') bb = gbb p = gp bb = apply_calib_net(image = image, iname = iname,bb=bb,nn=nn_calib12) cnt_detect = len(bb) #draw_bboxes(iname,bb,c='1-r_calib_12') keep = util.nms(bb,p,T=0.3) bb = bb[keep] if keep != [] else bb p = p[keep] if keep != [] else p #draw_bboxes(iname,bb,c='2-r_nms_12') bb,p = apply_24_48_net(image = image, iname = iname, bb = bb,nn = nn24,debug=0 ) cnt_detect = len(bb) #draw_bboxes(iname,bb,c='3-r_24') bb = apply_calib_net(image = image, iname = iname,bb = bb,nn=nn_calib24) cnt_detect = len(bb) #draw_bboxes(iname,bb,c='4-r_calib_24') keep = util.nms(bb,p,T=0.3) bb = bb[keep] if keep != [] else bb p = p[keep] if keep != [] else p #draw_bboxes(iname,bb,c='5-r_nms_24') cnt_detect = len(bb) bb,p = apply_24_48_net(image = image, iname = iname, bb = bb,nn = nn48,debug=1) cnt_detect = len(bb) draw_bboxes(iname,bb,c='6-r_48',debug=1) # bb = apply_calib_net(image = image,iname = iname,bb=bb,nn=nn_calib48) # draw_bboxes(iname,bb,c='6-r_calib_48') # # bb = util.nms(bb,T=0.2) # draw_bboxes(iname,bb,c='7-r_nms_48_final') print('Finish...',time()-s)
def cv(nn_name, d_num=10000, k_fold=7, score_metrics='accuracy', verbose=0): suff = str(nn_name[:2]) if nn_name.find('calib') > 0: X_data_name = 'train_data_icalib_' + suff + '.npy' y_data_name = 'labels_icalib_' + suff + '.npy' else: X_data_name = 'train_data_' + suff + '.npy' y_data_name = 'labels_' + suff + '.npy' X, y = sp.load(X_data_name), sp.load(y_data_name) d_num = min(len(X), d_num) X = X[:d_num] y = y[:d_num] rates12 = sp.hstack((0.05 * sp.ones(25, dtype=sp.float32), 0.005 * sp.ones(15, dtype=sp.float32), 0.0005 * sp.ones(10, dtype=sp.float32))) rates24 = sp.hstack((0.01 * sp.ones(25, dtype=sp.float32), 0.0001 * sp.ones(15, dtype=sp.float32))) rates48 = sp.hstack([ 0.05 * sp.ones(15, dtype=sp.float32), 0.005 * sp.ones(10, dtype=sp.float32) ]) if nn_name == '48-net': X12 = sp.load('train_data_12.npy')[:d_num] X24 = sp.load('train_data_24.npy')[:d_num] elif nn_name == '24-net': X12 = sp.load('train_data_12.npy')[:d_num] if score_metrics == 'accuracy': score_fn = accuracy_score else: score_fn = f1_score scores = [] iteration = 0 for t_indx, v_indx in util.kfold(X, y, k_fold=k_fold): nn = None X_train, X_test, y_train, y_test = X[t_indx], X[v_indx], y[t_indx], y[ v_indx] #print('\t \t',str(iteration+1),'fold out of ',str(k_fold),'\t \t' ) if nn_name == '24-net': nn = Cnnl( nn_name=nn_name, l_rates=rates24, subnet=Cnnl( nn_name='12-net', l_rates=rates12).load_model('12-net_lasagne_.pickle')) nn.fit(X=X_train, y=y_train, X12=X12[t_indx]) elif nn_name == '48-net': nn = Cnnl( nn_name=nn_name, l_rates=rates48, subnet=Cnnl( nn_name='24-net', l_rates=rates24, subnet=Cnnl( nn_name='12-net', l_rates=rates12).load_model('12-net_lasagne_.pickle')). load_model('24-net_lasagne_.pickle')) nn.fit(X=X_train, y=y_train, X12=X12[t_indx], X24=X24[t_indx]) else: nn = Cnnl(nn_name=nn_name, l_rates=rates12, verbose=verbose) nn.fit(X=X_train, y=y_train) if nn_name == '24-net': y_pred = nn.predict(X_test, X12=X12[v_indx]) elif nn_name == '48-net': y_pred = nn.predict(X_test, X12=X12[v_indx], X24=X24[v_indx]) else: y_pred = nn.predict(X_test) score = score_fn(y_test, y_pred) #print(iteration,'fold score',score) scores.append(score) iteration += 1 score_mean = sp.array(scores).mean() print(d_num, 'mean score', score) return score_mean