Exemplo n.º 1
0
 def random_image(self, image_path, label_path):  # line:31
     image_list, label_list = read_picture.read_image_data(image_path, label_path)  # line:33
     sample = random.sample(range(0, len(image_list)), 32)  # line:34
     print('随机取样完成')  # line:35
     color_dict = dict()  # line:36
     for real_img_index in sample:  # line:37
         color_dict[real_img_index] = random.randint(0, 2)  # line:38
     for img_index in range(len(sample)):  # line:39
         image_label = label_list[sample[img_index]]  # line:40
         for i in range(random.randint(0, len(image_list) // 2),
                        len(image_list)):  # line:43
             if label_list[i] == image_label and i not in color_dict:  # line:44
                 sample.append(i)  # line:45
                 color_dict[i] = color_dict[sample[img_index]]  # line:46
                 break  # line:47
     random.shuffle(sample)  # line:48
     image_pair = list()  # line:50
     for img_index in range(len(sample)):  # line:51
         real_img_index = sample[img_index]  # line:52
         image_data = image_list[real_img_index]  # line:53
         image_label = label_list[real_img_index]  # line:54
         color = color_dict[real_img_index]  # line:55
         read_picture.image_save(image_data, color, "../auto_grader/image/" + str(img_index) + ".png")  # line:57
         image_pair.append([color, image_label])  # line:58
     print('图片显示完毕')  # line:59
     return image_pair  # line:60
Exemplo n.º 2
0
 def random_image(O000O0OOO0OOOO000, O0OOOO00OO0O00000,
                  OOO0OOO0OO00000OO):  #line:31
     OO00OOO00OO00O0O0, O0000O000000O000O = read_picture.read_image_data(
         O0OOOO00OO0O00000, OOO0OOO0OO00000OO)  #line:33
     O00O000O00000O00O = random.sample(range(0, len(OO00OOO00OO00O0O0)),
                                       32)  #line:34
     print('随机取样完成')  #line:35
     O0OOO00OO0O00O000 = dict()  #line:36
     for OO0OO0OOOO0OOO0OO in O00O000O00000O00O:  #line:37
         O0OOO00OO0O00O000[OO0OO0OOOO0OOO0OO] = random.randint(0,
                                                               2)  #line:38
     for O0000O0O0O0O000O0 in range(len(O00O000O00000O00O)):  #line:39
         OOO0O000000O000OO = O0000O000000O000O[
             O00O000O00000O00O[O0000O0O0O0O000O0]]  #line:40
         for OOO0000OOO0OOOO00 in range(
                 random.randint(0,
                                len(OO00OOO00OO00O0O0) // 2),
                 len(OO00OOO00OO00O0O0)):  #line:43
             if O0000O000000O000O[
                     OOO0000OOO0OOOO00] == OOO0O000000O000OO and OOO0000OOO0OOOO00 not in O0OOO00OO0O00O000:  #line:44
                 O00O000O00000O00O.append(OOO0000OOO0OOOO00)  #line:45
                 O0OOO00OO0O00O000[OOO0000OOO0OOOO00] = O0OOO00OO0O00O000[
                     O00O000O00000O00O[O0000O0O0O0O000O0]]  #line:46
                 break  #line:47
     random.shuffle(O00O000O00000O00O)  #line:48
     OO00O0O00O0OO0OOO = list()  #line:50
     for O0000O0O0O0O000O0 in range(len(O00O000O00000O00O)):  #line:51
         OO0OO0OOOO0OOO0OO = O00O000O00000O00O[O0000O0O0O0O000O0]  #line:52
         OO00OOO0OO00OOOOO = OO00OOO00OO00O0O0[OO0OO0OOOO0OOO0OO]  #line:53
         OOO0O000000O000OO = O0000O000000O000O[OO0OO0OOOO0OOO0OO]  #line:54
         O000000O00O0OO0O0 = O0OOO00OO0O00O000[OO0OO0OOOO0OOO0OO]  #line:55
         read_picture.image_save(OO00OOO0OO00OOOOO, O000000O00O0OO0O0,
                                 "../auto_grader/image/" +
                                 str(O0000O0O0O0O000O0) + ".png")  #line:57
         OO00O0O00O0OO0OOO.append([O000000O00O0OO0O0,
                                   OOO0O000000O000OO])  #line:58
     print('图片显示完毕')  #line:59
     return OO00O0O00O0OO0OOO  #line:60
def identify_numbers(test_image):

    train_image, train_label = read_picture.read_image_data(
        './mnist_data/train-images.idx3-ubyte',
        './mnist_data/train-labels.idx1-ubyte')

    x_test, y_test = read_picture.read_image_data(
        './mnist_data/t10k-images.idx3-ubyte',
        './mnist_data/t10k-labels.idx1-ubyte')

    # (train_image, train_label), (x_test, y_test) = mnist.load_data()

    # train_image_arr = np.array(np.concatenate( [train_image , x_test], axis = 0 )) #array
    # train_label_arr = np.array(np.concatenate( [train_label , y_test], axis = 0 ))

    train_image_arr = np.array(
        np.concatenate([train_image, x_test[0:5000]], axis=0))  #array
    train_label_arr = np.array(
        np.concatenate([train_label, y_test[0:5000]], axis=0))

    # train_image_arr = np.array(train_image)  # before submission, comment these two lines
    # train_label_arr = np.array(train_label)  # before submission, comment these two lines

    large_test_image_arr = np.array(x_test)
    large_test_label_arr = np.array(y_test)
    test_image_arr = np.array(test_image)

    # print(len(train_label_arr))
    # print(len(train_image_arr))

    train_image_arr = np.reshape(train_image_arr, (-1, 784))
    test_image_arr = np.reshape(test_image_arr, (-1, 784))
    large_test_image_arr = np.reshape(large_test_image_arr, (-1, 784))

    # print(train_image_arr[0])
    # train_image_arr[train_image_arr <= 0] = 0  # 转化为黑白图像
    train_image_arr[train_image_arr > 0] = 255  # 转化为黑白图像
    #
    #
    # # test_image_arr[test_image_arr <= 0] = 0  # 转化为黑白图像
    test_image_arr[test_image_arr > 0] = 255  # 转化为黑白图像
    #
    # # large_test_image_arr[large_test_image_arr <= 0] = 0  # 转化为黑白图像
    large_test_image_arr[large_test_image_arr > 0] = 255  # 转化为黑白图像

    # mean_num = np.mean(train_image_arr, axis=0)
    # # print(mean_num)
    # train_image_arr = train_image_arr - mean_num
    # large_test_image_arr = large_test_image_arr - mean_num
    # test_image_arr = test_image_arr - mean_num

    # pca = PCA(n_components=100)
    #
    # pca.fit(train_image_arr)  # fit PCA with training data instead of the whole dataset
    # train_image_pca = pca.transform(train_image_arr)
    # test_image_pca = pca.transform(test_image_arr)
    # large_test_image_pca = pca.transform(large_test_image_arr)

    # train_image_pca[train_image_pca > 0] = 255  # 转化为黑白图像
    # test_image_pca[test_image_pca > 0] = 255  # 转化为黑白图像
    # large_test_image_pca[large_test_image_pca > 0] = 255  # 转化为黑白图像

    f = kNN.KNNClassify()
    f.fit(train_image_arr, train_label_arr)
    # f.fit(train_image_pca, train_label_arr)

    # train_image_arr_e, train_label_arr_e = f.edit(train_image_arr, train_label_arr)
    # train_image_arr_e, train_label_arr_e = f.edit(train_image_pca, train_label_arr)

    # train_image_arr_e = np.array(train_image_arr_e)
    # train_label_arr_e = np.array(train_label_arr_e)

    # edited_X = open("X_train_edited.txt", mode='wb')
    # edited_y = open("y_train_edited.txt", mode='wb')
    # X_dtype = train_image_arr_e.dtype
    # y_dtype = train_label_arr_e.dtype
    # X_shape = train_image_arr_e.shape
    # y_shape = train_label_arr_e.shape
    # print(X_dtype)
    # print(y_dtype)
    # print(X_shape)
    # print(y_shape)
    # edited_X.write(train_image_arr_e.tostring())
    # edited_y.write(train_label_arr_e.tostring())
    # train_image_arr, train_label_arr = f.edit(train_image_pca, train_label_arr)

    # edited_X = open("X_train_edited.txt", mode='rb')
    # edited_y = open("y_train_edited.txt", mode='rb')
    # train_image_arr = np.fromstring(edited_X.read(), dtype=getattr(np, "uint8")).reshape(70000,784)
    # train_label_arr = np.fromstring(edited_y.read(), dtype=getattr(np, "int32")).reshape(70000,)

    y_pre_1 = f.predict_y(large_test_image_arr[5000:6000], train_image_arr,
                          train_label_arr)
    #
    print(f.score(y_pre_1, large_test_label_arr[5000:6000]))

    # y_pre = f.predict_y(test_image_pca, train_image_pca, train_label_arr)
    # y_pre = f.predict_y(test_image_arr, train_image_arr_e, train_label_arr_e)
    y_pre = f.数字识别(test_image_arr, train_image_arr, train_label_arr)

    return y_pre
Exemplo n.º 4
0
                if ans_max < test_ans[num_i]:
                    ans_max = test_ans[num_i]
                    ans_i = num_i
                num_i += 1
            ans.append(self.num_kind[ans_i])
        ans = np.array(ans)
        return ans


if __name__ == "__main__":
    import sys
    sys.path.append("../read_picture/")
    import read_picture

    train_image, train_label = read_picture.read_image_data(
        '../../mnist_data/train-images.idx3-ubyte',
        '../../mnist_data/train-labels.idx1-ubyte')

    train_image_vector = np.reshape(train_image, (60000, 784))

    simple_train = simple_train_one_num(train_image_vector[0:5000],
                                        train_label[0:5000], 10, 0.1, 2.55)

    simple_train.train_learn()

    #构造测试集
    test_image_vector = train_image_vector[5000:5100]
    test_ans = train_label[5000:5100]
    #计算预测
    pre_ans = simple_train.predict(test_image_vector)