import scipy.ndimage as pyi import matplotlib.pyplot as plt import os base_dir = os.path.dirname(os.path.realpath(__file__)) imgs_dir = os.path.join(base_dir, 'hand') def read_hands_data(): imgs = np.ndarray((480, 512, 481)) for k in np.arange(481): img_name = 'hand.seq{}.png'.format(k + 1) img_dir = os.path.join(imgs_dir, img_name) imgs[:, :, k] = pyi.imread(img_dir) return imgs imgs = read_hands_data() resize_rate = 16 small_imgs = imgs[::resize_rate, ::resize_rate, :] feats = (small_imgs.reshape((int(np.size(small_imgs) / 481), 481))).T vs = get_eigenvalues(feats, NEIGHBOR=30, SAMPLE=20) #plt.plot(vs[:30].T) npzfile = np.load('large_data.npz') X = npzfile['X'] y = npzfile['y'] SSVR = SVR(C=0.6) SSVR.fit(X, y) pred_y = SSVR.predict(vs.flatten()[:100])
row, col = [480, 512] testdata = [] path = './hand/' for i in range(1, 482): testdata.append( misc.imread(path + 'hand.seq' + str(i) + '.png').reshape(row * col)) testdata = np.array(testdata) mu = testdata.mean(axis=0, keepdims=True) testdata_ctr = testdata - mu u, s, v = np.linalg.svd(testdata_ctr) reduceddata = np.dot(testdata_ctr, v[0:100].T) test_X = [] data = reduceddata.T vs = get_eigenvalues(data) test_X.append(vs) test_X = np.array(test_X) pred_y = svr.predict(test_X) pred_y[np.where(pred_y <= 1)] = 1 pred_y = np.round(pred_y) with open('ans.csv', 'w') as f: print('SetId,LogDim', file=f) for i, d in enumerate(pred_y): print(f'{i},{np.log(d)}', file=f) end = time.time() print('elapsed time = ', end - start)
# X /= X.max(axis=0, keepdims=True) svr = SVR(C=1) svr.fit(X, y) # svr.get_params() to save the parameters # svr.set_params() to restore the parameters # predict #testdata = np.load(sys.argv[1]); test_X = [] data_dir = "./hand" for filename in os.listdir(data_dir): loadfile = Image.open(os.path.join(data_dir, filename), 'r') loadfile.load() loadfile = loadfile.resize( (int(loadfile.size[0] / 8), int(loadfile.size[1] / 8))) data = np.asarray(loadfile, dtype="float32") data = np.reshape(data, (1, 64 * 60)) test_X.append(data) test_X = np.asarray(test_X) test_X = np.reshape(test_X, (481, 64 * 60)) vs = get_eigenvalues(test_X, 1) #test_X = np.array(test_X) pred_y = svr.predict(vs) pred_y = pred_y.astype(int) print(pred_y)