-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_convert_time_analysis_image_resize.py
More file actions
142 lines (108 loc) · 3.76 KB
/
Copy pathbinary_convert_time_analysis_image_resize.py
File metadata and controls
142 lines (108 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
"""Untitled7.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/121-mUurRjfF7bxXLmznJ3I35CX_WP49p
"""
import torch
import numpy as np
from PIL import Image
from torchvision import transforms
# Load the image
img = Image.open('/content/image4.jpg').convert('L') # Convert to grayscale
# Define transformations
preprocess = transforms.Compose([
transforms.Resize((5, 5)), # Resize to 5x5
transforms.ToTensor(),
])
# Apply transformations
img_tensor = preprocess(img)
# Convert to binary (assuming threshold of 0.5)
threshold = 0.5
binary_img = (img_tensor > threshold).float().byte()
# Convert binary_img to a format suitable for saving (e.g., list of lists)
binary_data = binary_img.tolist()
# Define the file path
file_path = '/content/binary value.txt'
# Write binary_data to a text file
with open(file_path, 'w') as file:
for row in binary_data:
file.write(' '.join(map(str, row)) + '\n')
print(f"Binary data saved to {file_path}")
from PIL import Image
import matplotlib.pyplot as plt
plt.imshow(img_tensor.squeeze().numpy(), cmap='gray')
plt.title('Resized 5x5 Image')
plt.axis('off')
plt.show()
# Step 1: Upload the text file
from google.colab import files
uploaded = files.upload()
# Step 2: Read the data from the text file
import numpy as np
# Assuming the uploaded file is named 'output.txt'
file_name = list(uploaded.keys())[0]
# Read the data from the file
with open(file_name, 'r') as file:
lines = file.readlines()
# Extract numerical data from the lines (adjust depending on your file format)
data = []
for line in lines:
if line.startswith("conv_out"):
value = int(line.split('=')[1].strip())
data.append(value)
# Convert the list to a numpy array and reshape to 3x3 matrix
matrix = np.array(data).reshape(3, 3)
print("Matrix:\n", matrix)
# Step 3: Visualize the data in grayscale
import matplotlib.pyplot as plt
# Create a heatmap to visualize the matrix in grayscale
plt.imshow(matrix, cmap='gray', interpolation='none')
plt.colorbar()
plt.title('Convolution Output (Grayscale)')
plt.show()
# Python code to read the log file and perform analysis
def read_time_log(file_path):
with open(file_path, 'r') as file:
lines = file.readlines()
times = [int(line.split(":")[1].strip().split()[0]) for line in lines]
return times
def analyze_times(times):
total_time = sum(times)
avg_time = total_time / len(times)
max_time = max(times)
min_time = min(times)
return {
'total_time': total_time,
'avg_time': avg_time,
'max_time': max_time,
'min_time': min_time
}
# Read the log file
times = read_time_log('time_log.txt')
# Perform analysis
analysis_results = analyze_times(times)
# Print results
print(f"Total Time: {analysis_results['total_time']} time units")
print(f"Average Time: {analysis_results['avg_time']} time units")
print(f"Max Time: {analysis_results['max_time']} time units")
print(f"Min Time: {analysis_results['min_time']} time units")
import numpy as np
import matplotlib.pyplot as plt
def read_output_matrix(file_path):
with open(file_path, 'r') as file:
lines = file.readlines()
# Extract the 8-bit values and convert to integers
values = [int(line.strip(), 2) for line in lines]
return values
def visualize_matrix(matrix, title="Downsampled Grayscale Image"):
plt.imshow(matrix, cmap='gray', vmin=0, vmax=255)
plt.colorbar()
plt.title(title)
plt.show()
# Read the 8-bit values from the output file
values = read_output_matrix('output_matrix.txt')
# Convert the flat list to a 2x2 numpy array (assuming the downsampled matrix is 2x2)
matrix = np.array(values).reshape((2, 2))
# Visualize the 2x2 matrix as a grayscale image
visualize_matrix(matrix)