def visualize(video_id: str, frame_id: str) -> None: annotation_input_path = ( personal_constants.DATASET_300VW_RAW_PATH / video_id / constants.DATASET_300VW_ANNOTATIONS_INPUT_FOLDER / f'{frame_id}.{constants.DATASET_300VW_ANNOTATIONS_INPUT_EXTENSION}' ) frame_input_path = ( personal_constants.DATASET_300VW_TEMP_PATH / video_id / constants.DATASET_300VW_IMAGES_TEMP_FOLDER / f'{frame_id}.{constants.DATASET_300VW_IMAGES_TEMP_EXTENSION}' ) image = cv2.imread(str(frame_input_path)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_landmarks = _load_pts_file(annotation_input_path) image_box = data_utils.landmarks_to_box(image_landmarks, image.shape) extracted_image = data_utils.extract(image, image_box) extracted_landmarks = data_utils.offset_landmarks(image_landmarks, image_box) output = _rescale_image(extracted_image) output_landmarks = data_utils.rescale_landmarks( extracted_landmarks, extracted_image.shape, constants.DATASET_300VW_IMSIZE ) # plot(image) # plot(image, image_landmarks) # plot(image, image_landmarks, image_box) # plot(extracted_image) # plot(extracted_image, extraction_landmarks) plot(output, output_landmarks)
def visualise(self, chunks): from data import plot to_draw = [] for c in chunks: to_draw.append(np.full((28, 1), 255)) to_draw.append(c) chunks = np.concatenate(to_draw, axis=1) plot(chunks[:, 1:], f"chunks")
def test_landmarks(arguments): video_path = Path('./data/local_data/300VW_Dataset_processed_dim128/516/') target_frame = 143 from_image = cv2.imread(str(video_path / 'images' / '000001.jpg')) all_landmarks = np.load(video_path / 'annotations.npy') multi_dim_landmarks = data_utils.single_to_multi_dim_landmarks( all_landmarks[target_frame - 1, :, :], constants.DATASET_300VW_IMSIZE, ) # plot(from_image) multi_dim_landmarks_orig = np.copy(multi_dim_landmarks) transform_to_input = [ transformations.Resize._f, transformations.RescaleValues._f, transformations.ChangeChannels._f, image_to_batch, lambda batch: batch.to(arguments.device), ] transform_from_input = [ image_from_batch, general_utils.de_torch, ] for t in transform_to_input: from_image = t(from_image) multi_dim_landmarks = t(multi_dim_landmarks) output = torch.cat((from_image, multi_dim_landmarks), dim=constants.CHANNEL_DIM) assert output.shape == (1, constants.INPUT_CHANNELS + constants.INPUT_LANDMARK_CHANNELS, constants.IMSIZE, constants.IMSIZE) model = get_model( arguments.use_model, arguments.model_base_path, arguments.model_name, arguments.model_date_path, arguments.device, ) output = model(output) for t in transform_from_input: output = t(output) multi_dim_landmarks = t(multi_dim_landmarks) output = general_utils.denormalize_picture(output) # output = output[:, :, :3] plot(output, landmarks_in_channel=multi_dim_landmarks)
def _test_augmentations(): from data.Dataset300VW import X300VWDataset dataset = X300VWDataset(constants.Dataset300VWMode.ALL, n_videos_limit=1) sample = dataset[0] image, landmarks = sample[0]['image'], sample[0]['landmarks'] plot(image, landmarks_in_channel=landmarks, title='original') for t in (RandomHorizontalFlip, RandomRescale, RandomCrop): sample = t(probability=1)(sample) image, landmarks = sample[0]['image'], sample[0]['landmarks'] plot(image, landmarks_in_channel=landmarks, title=t.__name__) transform = transforms.Compose([ RandomHorizontalFlip(probability=1), RandomRescale(probability=1), RandomCrop(probability=1), Resize(), RescaleValues(), ChangeChannels(), ]) sample = transform(sample) image, landmarks = sample[0]['image'], sample[0]['landmarks'] image = general_utils.move_color_channel(image) image = general_utils.denormalize_picture(image) landmarks = general_utils.move_color_channel(landmarks) plot(image, landmarks_in_channel=landmarks, title='all')
def _test_return() -> None: dataset = X300VWDataset(constants.Dataset300VWMode.ALL) n_images = len(dataset) dataset_indices = np.random.randint(0, n_images, size=3)[:3] for batch_index, dataset_index in enumerate(dataset_indices): batch = dataset[dataset_index] for sample_index, sample in enumerate(batch): image, landmarks = sample['image'], sample['landmarks'] assert image.shape == ( constants.DATASET_300VW_IMSIZE, constants.DATASET_300VW_IMSIZE, constants.INPUT_CHANNELS, ) assert landmarks.shape == ( constants.DATASET_300VW_IMSIZE, constants.DATASET_300VW_IMSIZE, constants.DATASET_300VW_N_LANDMARKS, ) print((batch_index, sample_index), image.shape, landmarks.shape) plot(image, landmarks_in_channel=landmarks) input('Press [enter] to exit.')
def evolution(): '''A star is initialised in the form of proton number, neutron number, and mass number matrices. Every iteration utilises weighted probabilities in randomising the movement of elements under gravity, as well as in determining the nuclear fusion reactions that occur. The matrix dimensions may be modified under the variable 'dim'. ''' # ignores runtime warning for handled ZeroDivisionError np.seterr(divide='ignore') # square matrix dimensions; minimum = 10 dim = 20 # proton number, neutron number, mass number matrices z, n, a = matrix.generate(dim) # energy matrix en = np.zeros((len(a), len(a))) # data handling arrays elm = np.array([1, 2, 6, 7, 8]) stack = np.zeros(len(elm)) # fusion rate array rate = [0] # total atomic mass print('\nMass: %d' % a.sum()) # output images directory if not os.path.exists('images'): os.mkdir('images') # initial system pos = matrix.positions(a) cm = matrix.centre_of_mass(a, pos) dens = density.matrix(a) density.plot(dens, 'initial') # data handling comp = data.composition(z, pos) for i in range(len(stack)): for j in range(len(comp)): if elm[i] == comp[j, 0]: stack[i] = comp[j, 1] break else: stack[i] = 0 elm = np.vstack((elm, stack)) data.log(comp, 'w', 0) # control variables flag1 = True flag2 = False iter = 0 print('\nIterations:\n') while flag1: iter += 1 print(iter, end='. ') # gravitation pos = matrix.positions(a) grav = gravity.force(a, pos) grav *= 1 - 0.99 * en.sum() for i in range(len(grav)): r = np.random.rand(1) j, k = pos[i] if r <= abs(grav[i, 0]): dir = int(grav[i, 0] / abs(grav[i, 0])) if a[j + dir, k] < a[j, k]: z[j, k], z[j + dir, k] = z[j + dir, k], z[j, k] n[j, k], n[j + dir, k] = n[j + dir, k], n[j, k] elif r <= np.abs(grav[i]).sum(): dir = int(grav[i, 1] / abs(grav[i, 1])) if a[j, k + dir] < a[j, k]: z[j, k], z[j, k + dir] = z[j, k + dir], z[j, k] n[j, k], n[j, k + dir] = n[j, k + dir], n[j, k] a = z + n # nuclear fusion ctr = 0 pos = matrix.positions(a) cm = matrix.centre_of_mass(a, pos) c_pos, c_temp = matrix.core(a, pos, cm) if not flag2: if c_temp > 7: flag2 = True else: for i in c_pos: j = i.copy() r = np.random.randint(2, size=2) # r[0] determines the axis (horizontal: 0, vertical: 1) # r[1] determines the direction along the axis dir = (-1)**r[1] if r[0]: j[0] += dir else: j[1] += dir p1 = [z[i[0], i[1]], n[i[0], i[1]]] p2 = [z[j[0], j[1]], n[j[0], j[1]]] try: f = c_temp / (p1[0] * p2[0]) if f > 1: f = 1 except ZeroDivisionError: f = 1 e = en[i[0], i[1]] nr = nuclear.reaction(p1, p2, f, e) z[i[0], i[1]] = nr[0] n[i[0], i[1]] = nr[1] z[j[0], j[1]] = nr[2] n[j[0], j[1]] = nr[3] a = z + n en[i[0], i[1]] = nr[4] if [p1, p2] != [[nr[0], nr[1]], [nr[2], nr[3]]]: ctr += 1 rate.append(ctr) # data handling comp = data.composition(z, pos) for i in range(len(stack)): for j in range(len(comp)): if elm[0, i] == comp[j, 0]: stack[i] = comp[j, 1] break else: stack[i] = 0 elm = np.vstack((elm, stack)) data.log(comp, 'a', iter) # iterates till H + He (stellar fuel) drops below 5.00% fuel = stack[0] + stack[1] print('Fuel: %.2f%%\n' % fuel) if fuel < 5: flag1 = False # final system pos = matrix.positions(a) cm = matrix.centre_of_mass(a, pos) dens = density.matrix(a) density.plot(dens, 'final') density.profile(dens, cm) data.plot(elm[1:], iter + 1) nuclear.plot_rate(rate, iter + 1)
def plot(self, show=False): data = self.asdata() data.plot() import pylab if show: pylab.show()
#!/usr/bin/python import data as dt import clusters as cl data = dt.fill_data("in/src_data.txt") print("=== INIT DATA ===") dt.print_data(data) data = dt.normalize(data) print("=== NORMALIZED DATA ===") dt.print_data(data) k_clusters = cl.k_averages(data, [4,2,8]) print("=== K AVERAGE ===") print(k_clusters) dt.plot("out/k_average.png", k_clusters, data) mm_clusters = cl.max_min(data) print("=== MAXIMIN ===") print(mm_clusters) dt.plot("out/maximin.png", mm_clusters, data)