Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def load_image(path):
img = skimage.io.imread(path)
resized_img = skimage.transform.resize(img, (IMAGE_SIZE, IMAGE_SIZE))
if resized_img.shape[2] != 3:
resized_img = resized_img[:, :, 0:3]
return resized_img
# im = Image.open("6_99CT.jpg")
# im1 = misc.imread("fake_B.png")
self.mask_B_mid = self.netE_B(torch.cat((self.fake_B_adj, self.fake_B_adj, self.fake_B_adj), 1))
self.mask_B = F.sigmoid(self.mask_B_mid) # 0-1
# if np.any(np.isnan(self.mask_B.cpu().detach().numpy())) == True:
# print('the raw data contain nan')
# print(self.data['A_paths'][0])
# misc.imsave("mask_B.gif", np.array(self.mask_B.cpu().detach().numpy()[0][0]))
# misc.imsave("mask_B_last.gif", np.array(self.mask_B_last.cpu().detach().numpy()[0][0]))
# self.mask_B = self.mask_B.unsqueeze(0)
self.mask_A_mid = io.imread(self.maskA_path)
# if np.any(np.isnan(self.mask_A_mid)) == True:
# print('the raw data contain nan')
# print(self.data['A_paths'][0])
self.mask_A = torch.from_numpy(transform.resize(self.mask_A_mid, (256, 256))).float().cuda().unsqueeze(0).unsqueeze(0)
# misc.imsave("mask_A.gif", self.mask_A)
def __call__(self, sample):
image, label = sample['image'], sample['label']
if isinstance(self.output_size, int):
new_h = new_w = self.output_size
else:
new_h, new_w = self.output_size
new_h, new_w = int(new_h), int(new_w)
# resize the image,
# preserve_range means not normalize the image when resize
img = transform.resize(image, (new_h, new_w), preserve_range=True, mode='constant')
label = transform.resize(label, (new_h, new_w), preserve_range=True, mode='constant')
return {'image': img, 'label': label}
def preprocess_image(self, img):
try:
from skimage.transform import resize
img = mpimg.imread(img)
if len(img.shape) == 3:
r, g, b = img[:, :, 0], img[:, :, 1], img[:, :, 2]
img = 0.2989 * r + 0.5870 * g + 0.1140 * b
#resized_image = cv2.resize(img, (28, 28))
resized_image = resize(img, (28, 28))
return self.__postprocess_image(resized_image)
except Exception as e:
self.config.logger.error(e)
return False
def _get_preprocessed_frame(self, observation):
"""
See Methods->Preprocessing in Mnih et al.
1) Get image grayscale
2) Rescale image
Note: Cannot take the max over the current and previous frame
as they do in the paper, because gym skips a random amount of
frames on each .act. Maybe try a workaround if performance
really suffers.
"""
return resize(rgb2gray(observation), (self.resized_width, self.resized_height))
def PreprocessImage(path, show_img=True):
# load image
img = io.imread(path)
# print("Original Image Shape: ", img.shape)
# we crop image from center
short_egde = min(img.shape[:2])
yy = int((img.shape[0] - short_egde) / 2)
xx = int((img.shape[1] - short_egde) / 2)
crop_img = img[yy : yy + short_egde, xx : xx + short_egde]
# resize to 299, 299
resized_img = transform.resize(crop_img, (299, 299))
if show_img:
io.imshow(resized_img)
# convert to numpy.ndarray
sample = np.asarray(resized_img) * 256
# swap axes to make image from (299, 299, 3) to (3, 299, 299)
sample = np.swapaxes(sample, 0, 2)
sample = np.swapaxes(sample, 1, 2)
# sub mean
normed_img = sample - 128.
normed_img /= 128.
return np.reshape(normed_img, (1, 3, 299, 299))
def resample_array(src_imgs, src_spacing, target_spacing):
src_spacing = np.round(src_spacing, 3)
target_shape = [int(src_imgs.shape[ix] * src_spacing[::-1][ix] / target_spacing[::-1][ix]) for ix in range(len(src_imgs.shape))]
for i in range(len(target_shape)):
try:
assert target_shape[i] > 0
except:
raise AssertionError("AssertionError:", src_imgs.shape, src_spacing, target_spacing)
img = src_imgs.astype(float)
resampled_img = resize(img, target_shape, order=1, clip=True, mode='edge').astype('float32')
return resampled_img
def pre_processing(observe):
processed_observe = np.uint8(
resize(rgb2gray(observe), (84, 84), mode='constant') * 255)
return processed_observe
def pre_processing(observe):
processed_observe = np.uint8(
resize(rgb2gray(observe), (84, 84), mode='constant') * 255)
return processed_observe
def im_rescale(im, output_size):
h, w = im.shape[:2]
if isinstance(output_size, int):
if h > w:
new_h, new_w = output_size * h / w, output_size
else:
new_h, new_w = output_size, output_size * w / h
else:
new_h, new_w = output_size
new_h, new_w = int(new_h), int(new_w)
img = transform.resize(im, (new_h, new_w), mode="constant")
return img, h, w, new_w / w, new_h / h