forked from nlintz/TensorFlow-Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson5.py
106 lines (76 loc) · 2.43 KB
/
lesson5.py
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
import tensorflow as tf
import numpy as np
session = tf.InteractiveSession()
#=== step1
#x = tf.constant(list(range(10)))
#print(x.eval())
#=== step2
# X = tf.constant(np.eye(10000))
# Y = tf.constant(np.random.randn(10000, 300))
# Z = tf.matmul(X, Y)
#=======exercise========
arr1=np.random.randint(256, size=1000000)
arr2=tf.to_float(arr1, name='ToFloat')
print(arr1.size)
# print(arr2.size)
#'Tensor' object has no attribute 'size'
# print(arr2.shape)
#'Tensor' object has no attribute 'shape'
print arr2
x=arr2.eval()
print(x.shape)
#===
import resource
print("{} Kb".format(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss))
session.close()
#===== exercise
from PIL import Image
from io import BytesIO
import matplotlib.image as mpimg
# First, load the image again
filename = "brain_left_right.jpg"
raw_image_data = Image.open(filename)
#raw_image_data = mpimg.imread(filename)
#===
image = tf.placeholder("uint8", [None, None, 3])
slice = tf.slice(image, [200, 0, 0], [100, -1, -1])
with tf.Session() as session:
result = session.run(slice, feed_dict={image: raw_image_data})
print(result.shape)
# read data from string
print(type(result))
bytes = result.tobytes()
print(type(bytes))
print(bytes[0:10])
#slicing (row index, col index, channel index) --> need wh reverse
wh=(result.shape[1], result.shape[0])
im=Image.frombuffer('RGB', wh, bytes, 'raw', 'RGB', 0, 1)
ms = BytesIO()
im.save(ms, format = "JPEG")
ms.flush()
ms.seek(0)
# readinto(b)
# Read up to len(b) bytes into b, and return the number of bytes read.
# The object b should be a pre-allocated, writable array of bytes, either bytearray or memoryview.
# arr=bytearray(len(bytes))
# ms.readinto(arr)
# print(arr[0:10])
im2 = Image.open(ms)
# try Image.open(open("path/to/file", 'rb'))
im2
# ==== log
"""
$ python lesson5.py
1000000
Tensor("ToFloat:0", shape=(1000000,), dtype=float32)
(1000000,)
107212 Kb
Exception AssertionError: AssertionError("Nesting violated for default stack of <type 'weakref'> objects",) in <bound method InteractiveSession.__del__ of <tensorflow.python.client.session.InteractiveSession object at 0x7fc20eeb6450>> ignored
(100, 550, 3)
Traceback (most recent call last):
File "lesson5.py", line 55, in <module>
im = Image.open(BytesIO(result))
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2317, in open
% (filename if filename else fp))
IOError: cannot identify image file <_io.BytesIO object at 0x7fc20c139b30>
"""