Пример #1
0
 def __init__(self):
     """
     LeNet classifier for greyscale images.  
     """
     self.net = Net()
     # -- Initialize layers ---
     weight_filler = Filler(type="xavier")
     bias_filler = Filler(type="constant", value=0.2)
     conv_lr_mults = [1.0, 2.0]
     conv_decay_mults = [1.0, 0.0]
     self.layer_list = []
     self.layer_list.append(Convolution(name="conv1", bottoms=["data"], param_lr_mults=conv_lr_mults, 
         param_decay_mults=conv_decay_mults, kernel_size=5, stride=1, pad=0, 
         weight_filler=weight_filler, bias_filler=bias_filler, num_output=20))
     self.layer_list.append(ReLU(name="relu1", bottoms=["conv1"], tops=["conv1"]))
     self.layer_list.append(Pooling(name="pool1", bottoms=["conv1"], kernel_size=2, stride=2))
     self.layer_list.append(Convolution(name="conv2", bottoms=["pool1"], param_lr_mults=conv_lr_mults, 
         param_decay_mults=conv_decay_mults, kernel_size=5, stride=1, pad=0, 
         weight_filler=weight_filler, bias_filler=bias_filler, num_output=50))
     self.layer_list.append(Pooling(name="pool2", bottoms=["conv2"], kernel_size=2, stride=2))
     self.layer_list.append(InnerProduct(name='ip1', bottoms=['pool2'],
         num_output=500, weight_filler=weight_filler))
     self.layer_list.append(ReLU(name="relu2", bottoms=["ip1"], tops=["ip1"]))
     self.layer_list.append(InnerProduct(name='ip2', bottoms=['pool2'],
         num_output=10, weight_filler=weight_filler))
     self.layer_list.append(SoftmaxWithLoss(name='softmax_loss', bottoms=['ip2', 'label']))
Пример #2
0
class LeNet:
    def __init__(self):
        """
        LeNet classifier for greyscale images.  
        """
        self.net = Net()
        # -- Initialize layers ---
        weight_filler = Filler(type="xavier")
        bias_filler = Filler(type="constant", value=0.2)
        conv_lr_mults = [1.0, 2.0]
        conv_decay_mults = [1.0, 0.0]
        self.layer_list = []
        self.layer_list.append(Convolution(name="conv1", bottoms=["data"], param_lr_mults=conv_lr_mults, 
            param_decay_mults=conv_decay_mults, kernel_size=5, stride=1, pad=0, 
            weight_filler=weight_filler, bias_filler=bias_filler, num_output=20))
        self.layer_list.append(ReLU(name="relu1", bottoms=["conv1"], tops=["conv1"]))
        self.layer_list.append(Pooling(name="pool1", bottoms=["conv1"], kernel_size=2, stride=2))
        self.layer_list.append(Convolution(name="conv2", bottoms=["pool1"], param_lr_mults=conv_lr_mults, 
            param_decay_mults=conv_decay_mults, kernel_size=5, stride=1, pad=0, 
            weight_filler=weight_filler, bias_filler=bias_filler, num_output=50))
        self.layer_list.append(Pooling(name="pool2", bottoms=["conv2"], kernel_size=2, stride=2))
        self.layer_list.append(InnerProduct(name='ip1', bottoms=['pool2'],
            num_output=500, weight_filler=weight_filler))
        self.layer_list.append(ReLU(name="relu2", bottoms=["ip1"], tops=["ip1"]))
        self.layer_list.append(InnerProduct(name='ip2', bottoms=['pool2'],
            num_output=10, weight_filler=weight_filler))
        self.layer_list.append(SoftmaxWithLoss(name='softmax_loss', bottoms=['ip2', 'label']))

    def fit(self, batch_iter, max_iter = 10000):
        training_manager = StepTraining(self.net, self.layer_list, batch_iter)
        loss_list = training_manager.set_iter(max_iter)
        return loss_list

    def predict(self, img):
        """
        Takes a 28 x 28 greyscale image as input and returns predicted label.  
        """
        self.net.reset_forward()
        self.net.phase = "test"
        image_blob = np.zeros((1, 1, 28, 28))
        image_blob[0][0] = img
        self.net.forward_layer(NumpyData(name = "data", data = image_blob))
        for layer in self.layer_list:
            if layer.deploy:
                self.net.forward_layer(layer)

        self.net.forward_layer(Softmax(name='softmax', bottoms=['ip2']))
        softmax_prob = self.net.tops["softmax"].data[0]
        return softmax_prob.argmax()