def _scatter(link, array, target): size, num = size_num_grads(link) ptrs = numpy.zeros(num, dtype=numpy.uint64) dtypes = numpy.zeros(num, dtype=numpy.int8) info = numpy.zeros(num + 1, dtype=numpy.int32) info[0] = 0 i = 0 for _, param in sorted(link.namedparams()): if param.size == 0: continue ptrs[i] = 0 # NULL pointer d = getattr(param, target) if d is None: d = cuda.cupy.zeros(param.shape, dtype=param.dtype) setattr(param, target, d) ptrs[i] = d.data.ptr dtypes[i] = 0 # fp32 if param.dtype == numpy.float16: dtypes[i] = 1 # fp16 info[i + 1] = info[i] + param.size i += 1 if i != num: raise() info[0] = num ptrs = cuda.to_gpu(ptrs) dtypes = cuda.to_gpu(dtypes) info = cuda.to_gpu(info) return _memcpy_scatter()(ptrs, dtypes, info, array, size=size)
def test_forward_gpu_train(self): self.rnn.to_gpu() with chainer.using_config('use_cudnn', 'always'), \ chainer.using_config('train', True): self.check_forward( cuda.to_gpu(self.h), [cuda.to_gpu(x) for x in self.xs])
def test_double_backward_gpu(self): self.check_double_backward( cuda.to_gpu(self.x1), cuda.to_gpu(self.x2), cuda.to_gpu(self.gy), cuda.to_gpu(self.ggx1), cuda.to_gpu(self.ggx2))
def test_double_backward_gpu(self): b = None if self.b is None else cuda.to_gpu(self.b) inputs = [cuda.to_gpu(self.x), cuda.to_gpu(self.W), b] grad_outputs = cuda.to_gpu([self.gy]) grad_grad_inputs = cuda.to_gpu([self.ggx, self.ggW, self.ggb]) self.check_double_backward( inputs, grad_outputs, grad_grad_inputs, use_cudnn='never')
def _gather(link, target): size, num = size_num_grads(link) ptrs = numpy.empty(num, dtype=numpy.uint64) dtypes = numpy.empty(num, dtype=numpy.int8) info = numpy.empty(num + 1, dtype=numpy.int32) info[0] = 0 i = 0 for _, param in sorted(link.namedparams()): if param.size == 0: continue ptrs[i] = 0 # NULL pointer d = getattr(param, target) if d is not None: ptrs[i] = d.data.ptr dtypes[i] = 0 # fp32 if param.dtype == numpy.float16: dtypes[i] = 1 # fp16 info[i + 1] = info[i] + param.size i += 1 info[0] = num ptrs = cuda.to_gpu(ptrs) dtypes = cuda.to_gpu(dtypes) info = cuda.to_gpu(info) return _memcpy_gather()(ptrs, dtypes, info, size=size)
def test_backward_chainerx_cuda_nobias(self): self._skip_if_not_chainerx_supported() self.check_backward( backend.to_chainerx(cuda.to_gpu(self.x)), backend.to_chainerx(cuda.to_gpu(self.W)), None, backend.to_chainerx(cuda.to_gpu(self.gy)))
def test_double_backward_gpu(self): self.check_double_backward( cuda.to_gpu(self.h), cuda.to_gpu(self.x), cuda.to_gpu(self.gy), cuda.to_gpu(self.ggh), cuda.to_gpu(self.ggx))
def test_backward_cpu_gpu(self): # This test compares gradients of CPU and GPU modes. rng = numpy.random.RandomState() rng_state = rng.get_state() # Call CPU mode link and save samples x = chainer.Variable(self.x) t = chainer.Variable(self.t) link = self.create_link(rng) y, samples = link(x, t, return_samples=True) y.backward() assert t.grad is None gw_cpu = link.W.grad gx_cpu = x.grad # Call GPU mode link rng.set_state(rng_state) link = self.create_link(rng) link.to_gpu() x = chainer.Variable(cuda.to_gpu(self.x)) t = chainer.Variable(cuda.to_gpu(self.t)) y = self.call_link_with_samples( cuda.to_gpu(samples), lambda: link(x, t)) y.backward() assert t.grad is None gw_gpu = link.W.grad gx_gpu = x.grad # Compare gradients from CPU and GPU modes testing.assert_allclose(gx_cpu, gx_gpu, **self.test_backward_options) testing.assert_allclose(gw_cpu, gw_gpu, **self.test_backward_options)
def test_double_backward_gpu(self): x1 = cuda.to_gpu(self.x1) x2 = cuda.to_gpu(self.x2) gy = cuda.to_gpu(self.gy) ggx1 = cuda.to_gpu(self.ggx1) ggx2 = cuda.to_gpu(self.ggx2) self.check_double_backward(x1, x2, gy, ggx1, ggx2)
def test_forward_output_size_zero(self, backend_config): if backend_config.use_chainerx: # TODO(sonots): Support it if self.dtype == numpy.float16: raise unittest.SkipTest('ChainerX does not support float16') with self.assertRaises(Exception): x = numpy.random.rand(4, 4, 1, 4).astype(self.dtype) # TODO(sonots): Cleanup to use testing.backend.get_array after # chainerx.asfortranarray is implemented. if (backend_config.use_cuda or (backend_config.use_chainerx and backend_config.chainerx_device.startswith('cuda:'))): x = cuda.to_gpu(x) if backend_config.use_chainerx: x = chainer.backend.to_chainerx(x) x = chainer.Variable(x) with backend_config: functions.max_pooling_2d(x, 3, stride=2) with self.assertRaises(Exception): x = numpy.random.rand(4, 4, 4, 1).astype(self.dtype) # TODO(sonots): Cleanup to use testing.backend.get_array after # chainerx.asfortranarray is implemented. if (backend_config.use_cuda or (backend_config.use_chainerx and backend_config.chainerx_device.startswith('cuda:'))): x = cuda.to_gpu(x) if backend_config.use_chainerx: x = chainer.backend.to_chainerx(x) x = chainer.Variable(x) with backend_config: functions.max_pooling_2d(x, 3, stride=2)
def test_batch_double_backward_gpu(self): x_data = cuda.to_gpu(self.x) y_grad = cuda.to_gpu(self.gy) x_grad_grad = cuda.to_gpu(self.ggx) gradient_check.check_double_backward( self.det, x_data, y_grad, x_grad_grad, **self.check_double_backward_options)
def test_backward_gpu(self): y = self.f(chainer.Variable(cuda.to_gpu(self.x)), chainer.Variable(cuda.to_gpu(self.x))) y.grad = cuda.to_gpu(self.gy) self.f.add_hook(self.h) y.backward() expect = r'''^function\tDummyFunction input data <variable at 0x[0-9a-f]+> - device: <CUDA Device 0> - backend: <(type|class) 'cupy.core.core.ndarray'> - shape: \(3L?, 5L?\) - dtype: float32 - statistics: mean=[0-9.\-e]+, std=[0-9.\-e]+ - grad: None \(removed\) output gradient <variable at 0x[0-9a-f]+> - device: <CUDA Device 0> - backend: <(type|class) 'cupy.core.core.ndarray'> - shape: \(3L?, 5L?\) - dtype: float32 - statistics: mean=[0-9.\-e]+, std=[0-9.\-e]+ - grad: mean=[0-9.\-e]+, std=[0-9.\-e]+$ ''' actual = self.io.getvalue() self.assertTrue(re.match(expect, actual), actual)
def check_double_backward( self, inputs, grad_outputs, grad_grad_inputs, backend_config): # TODO(sonots): Support it if backend_config.use_chainerx and self.dtype == numpy.float16: raise unittest.SkipTest('ChainerX does not support float16') # TODO(sonots): Cleanup to use testing.backend.get_array after # chainerx.asfortranarray is implemented. if (backend_config.use_cuda or (backend_config.use_chainerx and backend_config.chainerx_device.startswith('cuda:'))): inputs = cuda.to_gpu(inputs) grad_outputs = cuda.to_gpu(grad_outputs) grad_grad_inputs = cuda.to_gpu(grad_grad_inputs) if not self.c_contiguous: inputs = _to_fcontiguous(inputs) grad_outputs = _to_fcontiguous(grad_outputs) grad_grad_inputs = _to_fcontiguous(grad_grad_inputs) if backend_config.use_chainerx: inputs = chainer.backend.to_chainerx(inputs) grad_outputs = chainer.backend.to_chainerx(grad_outputs) grad_grad_inputs = chainer.backend.to_chainerx(grad_grad_inputs) def f(x): return functions.max_pooling_2d( x, 3, stride=2, pad=1, cover_all=self.cover_all) with backend_config: gradient_check.check_double_backward( f, inputs, grad_outputs, grad_grad_inputs, dtype='d', **self.check_double_backward_options)
def __call__(self, inputs, device=None): """Convert DALI arrays to Numpy/CuPy arrays""" xp = cuda.get_array_module(self.perturbation) if xp is not cuda.cupy: self.perturbation = cuda.to_gpu(self.perturbation, device) outputs = [] for i in range(len(inputs)): x = inputs[i].as_tensor() if (isinstance(x, dali.backend_impl.TensorCPU)): x = np.array(x) if x.ndim == 2 and x.shape[1] == 1: x = x.squeeze(axis=1) if device is not None and device >= 0: x = cuda.to_gpu(x, device) elif (isinstance(x, dali.backend_impl.TensorGPU)): x_cupy = cuda.cupy.empty(shape=x.shape(), dtype=x.dtype()) # Synchronization is necessary here to avoid data corruption # because DALI and CuPy will use different CUDA streams. cuda.cupy.cuda.runtime.deviceSynchronize() # copy data from DALI array to CuPy array x.copy_to_external(ctypes.c_void_p(x_cupy.data.ptr)) cuda.cupy.cuda.runtime.deviceSynchronize() x = x_cupy if self.perturbation is not None: x = x - self.perturbation if device is not None and device < 0: x = cuda.to_cpu(x) else: raise ValueError('Unexpected object') outputs.append(x) return tuple(outputs)
def test_sqnorm_array_multi_gpu(self): x0 = cuda.to_gpu(self.x, device=0) x1 = cuda.to_gpu(self.x, device=1) a0 = cuda.to_gpu(self.a, device=0) a1 = cuda.to_gpu(self.a, device=1) self.assertAlmostEqual(optimizer._sum_sqnorm( [self.x, self.a, x0, a0, x1, a1]), 8.75 * 3)
def test_backward_chainerx_cuda(self): # TODO(niboshi): Support it if self.dtype == numpy.float16: raise unittest.SkipTest('ChainerX does not support float16') self.check_backward_chainerx( backend.to_chainerx(cuda.to_gpu(self.x)), backend.to_chainerx(cuda.to_gpu(self.t)))
def check_backward(self, inputs, grad_outputs, backend_config): xp = backend_config.xp if backend_config.use_cuda: inputs = cuda.to_gpu(inputs) grad_outputs = cuda.to_gpu(grad_outputs) x_data, W_data, b_data = inputs y_grad, = grad_outputs if not self.c_contiguous: x_data = xp.asfortranarray(x_data) W_data = xp.asfortranarray(W_data) y_grad = xp.asfortranarray(y_grad) assert not x_data.flags.c_contiguous assert not W_data.flags.c_contiguous assert not y_grad.flags.c_contiguous if b_data is not None: b = xp.empty((len(b_data) * 2,), dtype=b_data.dtype) b[::2] = b_data b_data = b[::2] assert not b_data.flags.c_contiguous args = (x_data, W_data) if b_data is not None: args = args + (b_data,) def f(*args): return F.deconvolution_2d( *args, stride=self.stride, pad=self.pad, outsize=self.outsize, dilate=self.dilate, groups=self.groups) with backend_config: gradient_check.check_backward( f, args, y_grad, **self.check_backward_options)
def to_gpu(self): """Make a sampler GPU mode. """ if not self.use_gpu: self.threshold = cuda.to_gpu(self.threshold) self.values = cuda.to_gpu(self.values) self.use_gpu = True
def test_forward_gpu(self): self.link.to_gpu() x = cuda.to_gpu(self.x) if self.learn_b: b = None else: b = cuda.to_gpu(self.b) self.check_forward(x, b, self.y_expected)
def test_forward_gpu(self): self.link.to_gpu() x = cuda.to_gpu(self.x) if self.learn_W: W = None else: W = cuda.to_gpu(self.W) self.check_forward(x, W, self.y_expected)
def test_backward_non_default_gpu(self): x0 = chainer.Variable(cuda.to_gpu(self.x0, 1)) x1 = chainer.Variable(cuda.to_gpu(self.x1, 1)) gy = cuda.to_gpu(self.gy, 1) with cuda.get_device_from_id(0): y = functions.absolute_error(x0, x1) y.grad = gy y.backward()
def test_cupy_array2(self): x = cuda.to_gpu(self.x, device=self.device_dtype(0)) with x.device: if not self.c_contiguous: x = cuda.cupy.asfortranarray(x) y = cuda.to_gpu(x, device=self.device_dtype(1)) assert isinstance(y, cuda.ndarray) assert int(y.device) == 1
def test_forward_gpu(self): if not self.weight_apply: weight = None else: weight = cuda.to_gpu(self.class_weight) with chainer.using_config('use_cudnn', self.use_cudnn): self.check_forward( cuda.to_gpu(self.x), cuda.to_gpu(self.t), weight)
def test_debug_backward_gpu(self): self.f.forward_gpu = mock.MagicMock( return_value=(cuda.to_gpu(self.one),)) return_value = tuple(None if x is None else cuda.to_gpu(x) for x in self.return_value) input_value = (cuda.to_gpu(self.one),) * len(self.return_value) self.f.backward_gpu = mock.MagicMock(return_value=return_value) self.check_debug_backward(*input_value)
def test_double_backward_no_reduction_gpu(self): with chainer.using_config('use_cudnn', 'always'): self.check_double_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.t), cuda.to_gpu(self.gy), cuda.to_gpu(self.ggx), normalize=self.normalize, reduce='no')
def test_no_gh_backward_gpu(self): self.check_backward( cuda.to_gpu(self.c_prev1), cuda.to_gpu(self.c_prev2), cuda.to_gpu(self.x1), cuda.to_gpu(self.x2), cuda.to_gpu(self.gc), None)
def test_backward_gpu(self): self.rnn.to_gpu() with chainer.using_config('use_cudnn', 'auto'): self.check_backward( cuda.to_gpu(self.h), [cuda.to_gpu(x) for x in self.xs], cuda.to_gpu(self.gh), [cuda.to_gpu(gy) for gy in self.gys])
def test_forward_gpu_multi(self): with cuda.get_device_from_id(0): self.link.to_gpu() x1 = cuda.to_gpu(self.x1) x2 = cuda.to_gpu(self.x2) x3 = cuda.to_gpu(self.x3) with cuda.get_device_from_id(1): self.check_forward(x1, x2, x3)
def test_forward_gpu_multi(self): with cuda.get_device_from_id(0): self.link.to_gpu() c = cuda.to_gpu(self.c) h = cuda.to_gpu(self.h) x = cuda.to_gpu(self.x) with cuda.get_device_from_id(1): self.check_forward(c, h, x)
def check_backward(self, inputs, grad_outputs, backend_config): if backend_config.use_cuda: inputs = cuda.to_gpu(inputs) grad_outputs = cuda.to_gpu(grad_outputs) with backend_config: gradient_check.check_backward( functions.local_response_normalization, inputs, grad_outputs, eps=1, dtype=numpy.float64, **self.check_backward_options)
def get_scenes(): data = contrib.get_data() scenes = visualize_data() models = morefusion.datasets.YCBVideoModels() transform = [] points = [] sdf = [] pitch = [] origin = [] grid_target = [] grid_nontarget_empty = [] for instance in data["instances"]: transform.append(instance["transform_init"].astype(np.float32)) points_i, sdf_i = models.get_sdf(class_id=instance["class_id"]) points.append(cuda.to_gpu(points_i).astype(np.float32)) sdf.append(cuda.to_gpu(sdf_i).astype(np.float32)) pitch.append(instance["pitch"].astype(np.float32)) origin.append(instance["origin"].astype(np.float32)) grid_target.append(instance["grid_target"].astype(np.float32)) grid_nontarget_empty.append(instance["grid_nontarget_empty"].astype( np.float32)) pitch = cuda.cupy.asarray(pitch) origin = cuda.cupy.asarray(origin) grid_target = cuda.cupy.asarray(grid_target) grid_nontarget_empty = cuda.cupy.asarray(grid_nontarget_empty) link = morefusion.contrib.IterativeCollisionCheckLink(transform, sdf_offset=0.02) link.to_gpu() optimizer = chainer.optimizers.Adam(alpha=0.01) optimizer.setup(link) link.translation.update_rule.hyperparam.alpha *= 0.1 for i in tqdm.trange(100): transform = morefusion.functions.transformation_matrix( link.quaternion, link.translation) transform = cuda.to_cpu(transform.array) for j, instance in enumerate(data["instances"]): # cad = models.get_cad(instance["class_id"]) # if hasattr(cad.visual, "to_color"): # cad.visual = cad.visual.to_color() scenes["cad"].graph.update( frame_to=str(instance["id"]), frame_from="world", matrix=transform[j], ) # scenes["cad"].add_geometry( # cad, # node_name=str(instance["id"]), # geom_name=str(instance["id"]), # transform=transform[j], # ) yield {"icc": scenes["cad"]} loss = link(points, sdf, pitch, origin, grid_target, grid_nontarget_empty) loss.backward() optimizer.update() link.zerograds()
def run(which, steps, which_labels, frames, model, bg_model, face_model, optimizer, pred_diff, loss_saving, trait, ordered, save_all_results, twostream, same_frame, record_loss, record_predictions): print('steps: ', steps) assert (which in ['train', 'test', 'val']) assert (trait in ['O', 'C', 'E', 'A', 'S']) if which == 'train': which_batch_size = C.TRAIN_BATCH_SIZE elif which == 'val': which_batch_size = C.VAL_BATCH_SIZE elif which == 'test': which_batch_size = C.TEST_BATCH_SIZE loss_tmp = [] pd_tmp = np.zeros((steps, 1), dtype=float) _labs = list(which_labels) preds = np.zeros((steps, 1), dtype=float) if not ordered: shuffle(_labs) ts = time.time() for s in tqdm(range(steps)): labels_selected = _labs[s * which_batch_size:(s + 1) * which_batch_size] assert (len(labels_selected) == which_batch_size) labels_bg, bg_data, frame_num = D.load_data_single( labels_selected, which_labels, frames, which_data='bg', resize=True, ordered=ordered, twostream=twostream, same_frame=same_frame, trait=trait) labels_face, face_data, _ = D.load_data_single(labels_selected, which_labels, frames, which_data='face', resize=True, ordered=ordered, twostream=twostream, frame_num=frame_num, same_frame=same_frame, trait=trait) if C.ON_GPU: bg_data = to_gpu(bg_data, device=C.DEVICE) face_data = to_gpu(face_data, device=C.DEVICE) labels = to_gpu(labels_bg, device=C.DEVICE) with cp.cuda.Device(C.DEVICE): if which == 'train': config = True else: config = False with chainer.using_config('train', False): prediction_bg, bg_activations = bg_model(bg_data) prediction_face, face_activations = face_model(face_data) with chainer.using_config('train', config): if config: model.cleargrads() prediction = model(bg_activations, face_activations) loss = mean_absolute_error(prediction, labels) if which == 'train': loss.backward() optimizer.update() if record_loss: loss_tmp.append(float(loss.data)) pd_tmp[s] = U.pred_diff_trait(to_cpu(prediction.data), to_cpu(labels)) if record_predictions and which == 'test': preds[s] = to_cpu(prediction.data) if record_loss: pred_diff[0] = np.mean(pd_tmp, axis=0) loss_tmp_mean = np.mean(loss_tmp, axis=0) loss_saving.append(loss_tmp_mean) print('E %d. %s loss: ' % (0, which), loss_tmp_mean, ' pred diff %s: ' % trait, pred_diff[0], ' time: ', time.time() - ts) U.record_loss_sanity(which, loss_tmp_mean, pred_diff[0]) if which == 'test' and save_all_results: U.record_loss_all_test(loss_tmp, trait=True) if record_predictions and which == 'test': U.record_all_predictions(which, preds)
def to_xp(arrs): if backend_config.use_cuda: return cuda.to_gpu(arrs) else: return arrs
def test_backward_gpu(self): b = None if self.b is None else cuda.to_gpu(self.b) self.check_backward( cuda.to_gpu(self.x), cuda.to_gpu(self.W), b, cuda.to_gpu(self.gy), use_cudnn='never')
def test_rtol_gpu(self): self.check_rtol(cuda.to_gpu(self.x), cuda.to_gpu(self.y))
def test_invalid_reduce_gpu(self): self.check_invalid_reduce(cuda.to_gpu(self.x), cuda.to_gpu(self.t))
def test_forward_gpu(self): self.check_forward(cuda.to_gpu(self.x))
def test_double_backward_gpu(self): self.check_double_backward(cuda.to_gpu(self.x), self.output_shape, cuda.to_gpu(self.gy), cuda.to_gpu(self.ggx))
def predict_value_model_0(experiment_number, epoch, which='val', time_gap=1, model_number=0): use_ccc = True use_pearson = True _loss_steps = [] _all_ccc = [] _all_pearson = [] model = Siamese_pilot() models_path = '/scratch/users/gabras/data/omg_empathy/saving_data/models' p = os.path.join( models_path, 'model_%d_experiment_%d' % (model_number, experiment_number), 'epoch_%d' % epoch) chainer.serializers.load_npz(p, model) if C.ON_GPU: model = model.to_gpu(device=C.DEVICE) for subject in range(10): _loss_steps_subject = [] all_predictions = [] name = 'Subject_%d_Story_1' % (subject + 1) path = '/scratch/users/gabras/data/omg_empathy/Validation' subject_folder = os.path.join(path, 'jpg_participant_662_542', name) all_frames = os.listdir(subject_folder) full_name = os.path.join(path, 'Annotations', name + '.csv') all_labels = np.genfromtxt(full_name, dtype=np.float32, skip_header=True) # num_frames = len(all_frames) num_frames = 1000 _b = 0 _e = num_frames for f in range(0, num_frames): if f == 0: with cp.cuda.Device(C.DEVICE): with chainer.using_config('train', False): prediction = np.array([0.0], dtype=np.float32) # baseline labels = np.array([all_labels[f]]) loss = mean_squared_error(prediction, labels) _loss_steps_subject.append(float(loss.data)) all_predictions.append(0.0) else: data_left, data_right = L.get_left_right_consecutively( which, name, f, time_gap=time_gap) data_left = np.expand_dims(data_left, 0) data_right = np.expand_dims(data_right, 0) labels = np.array([all_labels[f]]) if C.ON_GPU: data_left = to_gpu(data_left, device=C.DEVICE) data_right = to_gpu(data_right, device=C.DEVICE) labels = to_gpu(labels, device=C.DEVICE) with cp.cuda.Device(C.DEVICE): with chainer.using_config('train', False): prediction = model(data_left, data_right) loss = mean_squared_error(prediction.data[0], labels) _loss_steps_subject.append(float(to_cpu(loss.data))) all_predictions.append(float(to_cpu(prediction.data))) if use_ccc: ccc_subject = calculate_ccc(all_predictions, all_labels[_b:_e]) _all_ccc.append(ccc_subject) print('%s, loss: %f, ccc: %f' % (name, float(np.mean(_loss_steps_subject)), ccc_subject)) if use_pearson: pearson_subject, p_vals = calculate_pearson( all_predictions, all_labels[_b:_e]) _all_pearson.append(pearson_subject) print('%s, loss: %f, pearson: %f' % (name, float(np.mean(_loss_steps_subject)), pearson_subject)) _loss_steps.append(np.mean(_loss_steps_subject)) print( 'model_%d_experiment_%d_epoch_%d, val_loss: %f, CCC: %f, pearson: %f' % (model_number, experiment_number, epoch, float(np.mean(_loss_steps)), float(np.mean(_all_ccc)), float(np.mean(_all_pearson))))
def forward(self, inputs): # Cause inconsistency between outputs return inputs[0], cuda.to_gpu(inputs[1])
def test_forward_gpu(self): self.check_forward(cuda.to_gpu(self.x), output_shape=self.output_shape[2:])
def test_reference_gpu(self): self.check_reference(cuda.to_gpu(self.x))
def test_numerical_grad_gpu(self): xs = tuple(map(cuda.to_gpu, self.xs)) gys = tuple(None if gy is None else cuda.to_gpu(gy) for gy in self.gys) self.check_invalid_eps(xs, gys, 0) self.check_invalid_eps(xs, gys, -1.0)
def test_invalid_mixed(self): y = cuda.to_gpu(self.y) with self.assertRaises(RuntimeError): gradient_check.numerical_grad(self.f, (self.x, ), (y, ))
def test_backward_consistency_regression_gpu(self): self.check_backward_consistency_regression(cuda.to_gpu(self.x), cuda.to_gpu(self.gy))
def test_identical_gpu(self): self.check_identical(cuda.to_gpu(self.x))
def test_backward_gpu(self): self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.gy))
def test_gpu(self): x = cuda.to_gpu(self.x) with chainer.using_config('use_cudnn', 'never'): self._check(x) with chainer.using_config('use_cudnn', 'always'): self._check(x)
def test_forward_consistency_regression_gpu(self): self.check_forward_consistency_regression(cuda.to_gpu(self.x))
def test_double_backward_gpu_no_cudnn(self): self.check_double_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.gy), cuda.to_gpu(self.ggx), 'never')
def test_array_supplied_gpu(self): self.check_array_supplied(cuda.to_gpu(self.x_data), cuda.to_gpu(self.W_data), cuda.to_gpu(self.b_data))
def test_backward_gpu(self): self.check_backward(cuda.to_gpu(self.x), self.output_shape, cuda.to_gpu(self.gy))
def test_value_check_gpu_cudnn(self): self.check_value_check(cuda.to_gpu(self.x), cuda.to_gpu(self.t), 'always')
def test_forward_gpu_test(self): self.rnn.to_gpu() with chainer.using_config('use_cudnn', 'always'), \ chainer.using_config('train', False): self.check_forward(cuda.to_gpu(self.h), cuda.to_gpu(self.c), [cuda.to_gpu(x) for x in self.xs])
def test_forward_consistency_regression_im2col(self): # Regression test to deconvolution_nd. if len(self.dims) == 2: self.check_forward_consistency_regression( cuda.to_gpu(self.x), cuda.to_gpu(self.W), cuda.to_gpu(self.b), use_cudnn='never')
def test_double_backward_gpu_non_contiguous(self): self.check_double_backward( cuda.cupy.asfortranarray(cuda.to_gpu(self.x)), cuda.cupy.asfortranarray(cuda.to_gpu(self.gy)), cuda.cupy.asfortranarray(cuda.to_gpu(self.ggx)))
def test_forward_gpu(self): self.link.to_gpu() self.check_forward(cuda.to_gpu(self.xs), cuda.to_gpu(self.ys))
def make_pred_vs_y_plot(experiment_number, epoch, which='val', time_gap=1, model_number=1, label_type='discrete'): use_ccc = True use_pearson = True _loss_steps = [] _all_ccc = [] _all_pearson = [] if model_number == 1: model = Siamese() elif model_number == 3: model = Triplet() elif model_number == 4: model = TernaryClassifier() models_path = '/scratch/users/gabras/data/omg_empathy/saving_data/models' p = os.path.join( models_path, 'model_%d_experiment_%d' % (model_number, experiment_number), 'epoch_%d' % epoch) chainer.serializers.load_npz(p, model) if C.ON_GPU: model = model.to_gpu(device=C.DEVICE) for subject in range(10): _loss_steps_subject = [] previous_prediction = np.array([0.], dtype=np.float32) all_predictions = [] name = 'Subject_%d_Story_1' % (subject + 1) path = '/scratch/users/gabras/data/omg_empathy/Validation' subject_folder = os.path.join(path, 'jpg_participant_662_542', name) all_frames = os.listdir(subject_folder) if label_type == 'discrete': full_name = os.path.join(path, 'Annotations', name + '.csv') elif label_type == 'smooth': full_name = os.path.join(path, C.SMOOTH_ANNOTATIONS_PATH, name + '.csv') all_labels = np.genfromtxt(full_name, dtype=np.float32, skip_header=True) # num_frames = len(all_frames) num_frames = 50 # num_frames = 10 if time_gap > 1: _b = C.OMG_EMPATHY_FRAME_RATE _e = _b + num_frames else: _b = 0 _e = num_frames # for f in tqdm(range(_b, _e)): for f in range(_b, _e): if f == 0: with cp.cuda.Device(C.DEVICE): with chainer.using_config('train', False): prediction = np.array([0.0], dtype=np.float32) # baseline labels = np.array([all_labels[f]]) loss = mean_squared_error(prediction, labels) _loss_steps_subject.append(float(loss.data)) else: data_left, data_right = L.get_left_right_consecutively( which, name, f, time_gap=time_gap) data_left = np.expand_dims(data_left, 0) data_right = np.expand_dims(data_right, 0) labels = np.array([all_labels[f]]) if C.ON_GPU: previous_prediction = to_gpu(previous_prediction, device=C.DEVICE) data_left = to_gpu(data_left, device=C.DEVICE) data_right = to_gpu(data_right, device=C.DEVICE) labels = to_gpu(labels, device=C.DEVICE) with cp.cuda.Device(C.DEVICE): with chainer.using_config('train', False): if model_number == 3: pred_1, pred_2 = model(data_left, data_right) pred_1 = chainer.functions.sigmoid(pred_1) pred_1 = U.threshold_all(to_cpu(pred_1.data)) prediction = to_gpu(pred_1, device=C.DEVICE) * pred_2 prediction = previous_prediction + prediction loss = mean_squared_error(prediction.data[0], labels) _loss_steps_subject.append(float(loss.data)) prediction = float(prediction.data) else: prediction = model(data_left, data_right) prediction = previous_prediction + prediction loss = mean_squared_error(prediction.data[0], labels) _loss_steps_subject.append(float(loss.data)) prediction = float(prediction.data) previous_prediction = to_cpu(previous_prediction) previous_prediction[0] = float(prediction) all_predictions.append(prediction) # if use_ccc: # ccc_subject = calculate_ccc(all_predictions, all_labels[_b:_e]) # _all_ccc.append(ccc_subject) # print('%s, loss: %f, ccc: %f' % (name, float(np.mean(_loss_steps_subject)), ccc_subject)) # # if use_pearson: # pearson_subject, p_vals = calculate_pearson(all_predictions, all_labels[_b:_e]) # _all_pearson.append(pearson_subject) # print('%s, loss: %f, pearson: %f' % (name, float(np.mean(_loss_steps_subject)), pearson_subject)) # # _loss_steps.append(np.mean(_loss_steps_subject)) # save graph save_graph = True if save_graph: labs = all_labels[_b:_e] diff_arr = labs - all_predictions diff_matrix = np.zeros((num_frames, num_frames)) for i in range(num_frames): diff_matrix[i, i] = diff_arr[i] p = '/scratch/users/gabras/data/omg_empathy/saving_data/logs/val/pred_vs_y' plots_folder = 'model_%d_experiment_%d' % (model_number, experiment_number) plot_path = os.path.join(p, plots_folder) if not os.path.exists(plot_path): os.mkdir(plot_path) fig = plt.figure() # assert len(labs) == len(all_predictions) # plt.scatter(labs, all_predictions) fig, ax = plt.subplots() im = ax.imshow(diff_matrix) # We want to show all ticks... ax.set_xticks(np.arange(len(diff_arr))) ax.set_yticks(np.arange(len(diff_arr))) # ... and label them with the respective list entries # ax.set_xticklabels(farmers) # ax.set_yticklabels(vegetables) # Rotate the tick labels and set their alignment. # plt.setp(ax.get_xticklabels(), rotation=45, ha="right", # rotation_mode="anchor") # Loop over data dimensions and create text annotations. # for i in range(len(diff_arr)): # for j in range(len(diff_arr)): # text = ax.text(j, i, str(diff_matrix[i, j])[0:7], ha="center", va="center", color="w") ax.set_title('difference: labels - predictions') fig.tight_layout() plt.savefig( os.path.join(plot_path, '%s_epoch_%d_.png' % (name, epoch)))
def test_debug_forward_gpu(self): return_value = tuple(None if x is None else cuda.to_gpu(x) for x in self.return_value) self.f.forward_gpu = mock.MagicMock(return_value=return_value) self.check_debug_forward(cuda.to_gpu(self.one))
def test_differenct_eps_gpu(self): self.check_different_eps(cuda.to_gpu(self.x), cuda.to_gpu(self.y))