def __forward(self, train, a_batch, q_batch, y_batch = None): n_units = self.__n_units mod = self.__mod gpu = self.__gpu batch_size = len(a_batch) x_len = len(a_batch[0]) if gpu >=0: a_batch = [[mod.array(e) for e in row] for row in a_batch] self.reset_state() x_data = mod.concatenate([mod.transpose(mod.concatenate(a_batch[b], axis=0)).reshape((1,n_units,1,x_len)) for b in range(batch_size)], axis=0) x = Variable(x_data, volatile=not train) x = F.reshape(x, (batch_size,n_units,x_len)) M_t = F.swapaxes(x, 1, 2) full_shape = (batch_size, x_len, n_units) for l in range(x_len): x_t = [] for b in range(batch_size): x = a_batch[b][l] x_t.append(x) x_t = Variable(mod.concatenate(x_t, axis=0), volatile=not train) o_t, m_t, z_t = self.read(M_t, x_t, batch_size, train) c_t = self.compose(o_t, m_t, train) M_t, h_t = self.write(M_t, c_t, z_t, full_shape, train) if gpu >=0: q_batch = [[mod.array(e) for e in row] for row in q_batch] x_data = mod.concatenate([mod.transpose(mod.concatenate(q_batch[b], axis=0)).reshape((1,n_units,1,x_len)) for b in range(batch_size)], axis=0) x = Variable(x_data, volatile=not train) x = F.reshape(x, (batch_size,n_units,x_len)) M2_t = F.swapaxes(x, 1, 2) for l in range(x_len): x_t = [] for b in range(batch_size): x = q_batch[b][l] x_t.append(x) x_t = Variable(mod.concatenate(x_t, axis=0), volatile=not train) o_t, m_t, z_t, m2_t, z2_t = self.read2(M_t, M2_t, x_t, batch_size, train) c_t = self.compose2(o_t, m_t, m2_t, train) M_t, M2_t, h2_t = self.write2(M_t, M2_t, c_t, z_t, z2_t, full_shape, train) hs = F.concat([F.concat([h_t, h2_t], axis=1), h_t-h2_t, h_t*h2_t], axis=1) hs = F.relu(self.h_l1(hs)) y = self.l_y(F.dropout(hs, ratio=0.3, train=train)) preds = mod.argmax(y.data, 1).tolist() accum_loss = 0 if train else None if train: if gpu >= 0: y_batch = cuda.to_gpu(y_batch) lbl = Variable(y_batch, volatile=not train) accum_loss = F.softmax_cross_entropy(y, lbl) return preds, accum_loss
for i in range(0, len(index_test), BATCH_SIZE): Text = [] Label = [] Feature = [] for n in range(BATCH_SIZE): if i + n >= (len(index_test)): text = index_test[i + n - len(index_test)][0] label = index_test[i + n - len(index_test)][1] feature = index_test[i + n - len(index_test)][2] else: text = index_test[i + n][0] label = index_test[i + n][1] feature = index_test[i + n][2] Text.append(text) Label.append(label) Feature.append(feature) Text = np.array(Text, dtype="int32") Label = np.array(Label, dtype="int32") Feature = np.array(Feature, dtype="float32") Feature = np.mat(Feature) Feature = Feature.reshape(-1, 1) Feature = np.array(Feature) # print("feature vector = ", Feature) Feature = Variable(Feature) model.cleargrads() loss = model(Text, Label, Feature) #loss.backward() #optimizer.update()
def __call__(self, x, t=None): on_gpu = isinstance(x.data, cupy.ndarray) self.cae_ones1.train = self.train self.vgg2.train = self.train roi_scale = self.cae_ones1.encode(x) if t is None: assert self.train is False if on_gpu: roi_scale_data = cuda.to_cpu(roi_scale.data) rois_data = self.initial_roi * roi_scale_data if on_gpu: rois_data = cuda.to_cpu(rois_data) x_data = cuda.to_cpu(x.data) rois_data = rois_data.astype(int) cropped = [] for i in xrange(len(x_data)): roi = rois_data[i] im = blob_to_im(x_data[i]) im = im[roi[0]:roi[2], roi[1]:roi[3]] im = resize(im, (128, 128), preserve_range=True) cropped.append(im_to_blob(im)) cropped = np.array(cropped, dtype=np.float32) if on_gpu: cropped = cuda.to_gpu(cropped) cropped = Variable(cropped, volatile=not self.train) self.vgg2(cropped) self.y = self.vgg2.y return self.y # randomly change the param and estimate good parameter for the task min_y = None rands_shape = [self.learning_n_sample] + list(roi_scale.data.shape) rands = self.learning_rate * (2 * np.random.random(rands_shape) - 1) + 1 rands[0] = np.ones(roi_scale.data.shape) for i, rand in enumerate(rands): if on_gpu: roi_scale_data = cuda.to_cpu(roi_scale.data) rois_data = rand * (self.initial_roi * roi_scale_data) x_data = cuda.to_cpu(x.data) skip = False rois_data = rois_data.astype(int) cropped = [] for j in xrange(len(x_data)): roi = rois_data[j] im = blob_to_im(x_data[j]) im = im[roi[0]:roi[2], roi[1]:roi[3]] if im.size == 0: skip = True break im = resize(im, (128, 128), preserve_range=True) cropped.append(im_to_blob(im)) if skip: continue cropped = np.array(cropped) if on_gpu: cropped = cuda.to_gpu(cropped) cropped = Variable(cropped, volatile=not self.train) self.vgg2(cropped, t) h = self.vgg2.y loss = F.softmax_cross_entropy(h, t) if min_y is None: min_loss_data = float(loss.data) min_y = h min_loss = loss min_rand = rand min_rois = rois_data elif min_loss_data > float(loss.data): min_loss_data = float(loss.data) min_y = h min_loss = loss min_rand = rand min_rois = rois_data if on_gpu: min_rand = cuda.to_gpu(min_rand) rois_data = min_rand * roi_scale.data xp = cuda.get_array_module(rois_data) rois_data = rois_data.astype(xp.float32) rois = Variable(rois_data, volatile=not self.train) loss1 = F.mean_squared_error(roi_scale, rois) loss2 = min_loss self.loss = loss1 + loss2 self.accuracy = F.accuracy(min_y, t) return self.loss
x = [] for i in range(5): x.append(atrain[r:r + train_size]) r = random.choice([0, alen - train_size]) x = Variable(np.array(x).astype(np.float32)) y = Variable( np.array([[1, 0], [1, 0], [1, 0], [1, 0], [1, 0]]).astype(np.float32)) #aはゼロ model.cleargrads() #勾配初期化 loss = model(x, y) #誤差計算 loss.backward() #勾配計算 optimizer.update() #パラメータ更新 r = random.choice([0, blen - train_size]) x = [] for i in range(5): x.append(atrain[r:r + train_size]) r = random.choice([0, alen - train_size]) x = Variable(np.array(x).float32) y = Variable( np.array([[0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]).astype(np.float32)) #bは1 model.cleargrads() #勾配初期化 loss = model(x, y) #誤差計算 loss.backward() #勾配計算 optimizer.update() #パラメータ更新 if j % 10 == 0: print(j, "/", epoch_count, "回終了") #####結果の出力########################## xt = Variable(make_wakati(恥の多い生涯を送って来ました)) yt = model.fwd(xt)
cv2.waitKey(50) #print('shape', img2.shape[:2]) img1 = img1[:, :, :3] img1 = np.asarray(img1, dtype=np.float32) / 255.0 img1 = img1.transpose(2, 0, 1) img2 = img2[:, :, :3] img2 = np.asarray(img2, dtype=np.float32) / 255.0 img2 = img2.transpose(2, 0, 1) # load model #model.predictor.train = False # forward x = [] x.append(np.array(img1)) x.append(np.array((img2))) x = np.asarray(x, dtype=np.float32) x = Variable(x) if hasattr(cuda, "cupy"): print('hasatt inside') cuda.get_device(0).use() model.to_gpu() x.to_gpu() one_hot_t = [] first = [0, 0, 0, 1] secund = [0, 0, 1, 0] one_hot_t.append(np.array(first)) one_hot_t.append(np.array(secund))