Exemple #1
0
def Cal_Angle(img, detection_net=_DETECTION, angle_net=_ANGLE):
    if Decaf:
        scores = detection_net.classify(img, False)
        is_car = detection_net.top_k_prediction(scores, 1)
        #print is_car
        if is_car[1][0] == 'car':
            car_conv3 = detection_net.feature("conv3_neuron_cudanet_out")
            mid_convs = car_conv3.reshape((car_conv3.shape[0],-1))
            scores = angle_net.classify(mid_convs)
            angles = angle_net.top_k_prediction(scores, 3)
            print angles
            
            o = float(angles[1][0])-90 # *180/np.pi
            o = o if o>0 else o+360        
            return int(o)
    else:
        scores = car_model.show_predictions(img[:,:,0])
        print scores
        if scores[-1][0] == 'car':
            mid_convs = car_model.get_features(img[:,:,0])
            scores = angle_net.classify(mid_convs)
            angles = angle_net.top_k_prediction(scores, 3)
            print angles
            
            o = float(angles[1][0])-90 # *180/np.pi
            o = o if o>0 else o+360        
            return int(o)
    return None
def Cal_Angle(img, detection_net=_DETECTION, angle_net=_ANGLE):
    if Decaf:
        scores = detection_net.classify(img, False)
        is_car = detection_net.top_k_prediction(scores, 1)
        # print is_car
        if is_car[1][0] == "car":
            car_conv3 = detection_net.feature("conv3_neuron_cudanet_out")
            mid_convs = car_conv3.reshape((car_conv3.shape[0], -1))
            scores = angle_net.classify(mid_convs)
            angles = angle_net.top_k_prediction(scores, 3)
            print angles

            o = float(angles[1][0]) - 90  # *180/np.pi
            o = o if o > 0 else o + 360
            return int(o)
    else:
        scores = car_model.show_predictions(img[:, :, 0])
        print scores
        if scores[-1][0] == "car":
            mid_convs = car_model.get_features(img[:, :, 0])
            scores = angle_net.classify(mid_convs)
            angles = angle_net.top_k_prediction(scores, 3)
            print angles

            o = float(angles[1][0]) - 90  # *180/np.pi
            o = o if o > 0 else o + 360
            return int(o)
    return None
def Cal_Angle(img, detection_net=_DETECTION, angle_net=_ANGLE):
    # TODO: prob filter 0.95会不会太严格
    if Decaf:
        scores = detection_net.classify(img, False)
        is_car = detection_net.top_k_prediction(scores, 1)
        #print is_car
        if is_car[1][0] == 'car' and is_car[0][0] >= 0.9:
            car_conv3 = detection_net.feature("conv3_neuron_cudanet_out")
            mid_convs = car_conv3.reshape((car_conv3.shape[0],-1))
            scores = angle_net.classify(mid_convs)
            angles = angle_net.top_k_prediction(scores, 3)
            
            o = float(angles[1][0])-90 # *180/np.pi
            o = o if o>0 else o+360        
            return int(o), is_car[0][0]
    else:
        is_car = car_model.show_predictions(img[:,:,0])
        # print is_car
        if is_car[-1][0] == 'car' and is_car[-1][1] >= 0.9:
            mid_convs = car_model.get_features(img[:,:,0])
            scores = angle_net.classify(mid_convs)
            angles = angle_net.top_k_prediction(scores, 3)
            #print angles
            
            o = float(angles[1][0])-90 # *180/np.pi
            o = o if o>0 else o+360
            return int(o), is_car[-1][1]
    return None
 else:
     print "Using " + sys.argv[1]
     car = imread(sys.argv[1])
 
 Decaf = False
 if Decaf:
     from kitnet import DecafNet as KitNet
     kit_net = KitNet()
 
     # print car.shape
     car = car.reshape((40,40,1))
     scores = kit_net.classify(car)
     print 'Is car ? prediction:', kit_net.top_k_prediction(scores, 1)
     
     car_conv3 = kit_net.feature("conv3_neuron_cudanet_out") #conv3_cudanet_out
     mid_convs = car_conv3.reshape((car_conv3.shape[0],-1))
 else:
     os.chdir("E:/2013/cuda-convnet/trunk")
     # sys.path.append("E:/2013/cuda-convnet/trunk")
     from show_pred import model as car_model
     scores = car_model.show_predictions(car)
     print 'Is car ? prediction:', scores[-1]
     
     mid_convs = car_model.get_features(car)
     os.chdir(os.path.dirname(os.path.realpath(__file__)))
 # visualize.draw_net_to_file(net._net, "decafnet.png") #bug!
 # print 'Network structure written to decafnet.png'
 net = DecafNet()
 scores = net.classify(mid_convs)
 print 'Direction ? prediction:',  net.top_k_prediction(scores, 5)#scores*180