First of all I am completely new to Python so, any help regarding the following would be much appreciated. I perform some test coding in Python (using pycharm as a front end), which in turn calls machine learning functions in keras.
I have created a virtual environment, base interpreter and managed to run the first test code. However, when I run the second code (attached below), I get the following error (also attached below).
This error most possibly has to do with the so called 'hypot' declaration as described in this question: compile some code with boost.python by mingw in win7-64bit. The accepted answer provided mentions: "To fix that, #include before including the boost/python headers."
If that's what I also have to do, could you please help me how to 'include cmath before including the boost/python headers'? Any ideas would be very much appreciated.
CODE
# Segmentation demo
#import PIL
#from keras.utils import plot_model
# from keras.utils import plot_model
import keras.backend as K
import matplotlib.pyplot as plt
import numpy as np
from PIL import ImageDraw, Image
from keras.layers import Input, Conv2D
from keras.models import Model
# import PIL
# Generate random data
def generate_data(num):
x, y = [], []
for i in range(num):
im = Image.new('RGB', (50, 50), (0, 0, 0))
im_mask = Image.new('RGB', (50, 50), (0, 0, 0))
draw = ImageDraw.Draw(im)
draw_mask = ImageDraw.Draw(im_mask)
num_shapes = 7
edge_max = 8
max_coord = 45
# rectangles
for i in range(np.random.randint(2, num_shapes)):
edge = np.random.randint(2, edge_max)
tl = (np.random.randint(max_coord), np.random.randint(max_coord))
tr = (tl[0], tl[1] + edge)
bl = (tl[0] + edge, tl[1])
br = (tl[0] + edge, tl[1] + edge)
draw.polygon([tl, bl, br, tr], fill="red")
draw_mask.polygon([tl, bl, br, tr], fill="white")
# lines
for i in range(np.random.randint(2, num_shapes)):
edge = np.random.randint(2, edge_max)
a = (np.random.randint(max_coord - edge), np.random.randint(max_coord - edge))
b = (a[0] + np.random.randint(2 * edge), a[1] + np.random.randint(2 * edge))
draw.line((a, b), fill="red")
# ellipses
# for i in range(np.random.randint(2, num_shapes)):
# edge = np.random.randint(2, edge_max)
# tl = (np.random.randint(max_coord), np.random.randint(max_coord))
# br = (tl[0] + edge, tl[1] + edge)
# draw.ellipse([tl, br], fill="red")
# pentagons
for i in range(np.random.randint(2, num_shapes)):
edge = np.random.randint(2, edge_max)
tl2 = (np.random.randint(max_coord), np.random.randint(max_coord))
tr = (tl2[0], tl2[1] + np.random.randint(edge))
bl1 = (tl2[0] + np.random.randint(edge), tl2[1] + np.random.randint(edge))
bl2 = (bl1[0] + np.random.randint(edge), bl1[1] + np.random.randint(edge))
br = (bl2[0] + np.random.randint(edge), bl2[1] + np.random.randint(edge))
draw.polygon([tl2, bl1, bl2, br, tr], fill="red")
x.append(np.reshape(im.getdata(), (50, 50, 3)).T)
y.append(np.reshape(im_mask.getdata(), (50, 50, 3)).T[0:1, :, :])
x = np.array(x)
y = np.array(y)
return x, y
def plot_ims(model):
x, y = generate_data(10)
y[y == 255] = 1
ypred = model.predict(x)
plt.figure()
for i in range(0, 6):
plt.subplot(2, 6, i * 2 + 1)
plt.imshow(x[i * 2 - 1, 0])
plt.xticks([])
plt.yticks([])
plt.subplot(2, 6, i * 2 + 2)
plt.imshow(ypred[i * 2 - 1, 0], cmap=plt.cm.gray)
plt.xticks([])
plt.yticks([])
def dsc(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * intersection + 1.) / (K.sum(y_true_f) + K.sum(y_pred_f) + 1.)
def dsc_loss(y_true, y_pred):
return 1. - dsc(y_true, y_pred)
# Define Model
inp = Input(shape=(3, 50, 50))
l = Conv2D(8, 3, activation='relu', padding='same')(inp)
l = Conv2D(8, 3, activation='relu', padding='same')(l)
l = Conv2D(8, 3, activation='relu', padding='same')(l)
l = Conv2D(8, 3, activation='relu', padding='same')(l)
out = Conv2D(1, 1, activation='sigmoid', padding='same')(l)
model = Model(inputs=inp, outputs=out)
model.summary()
#plot_model(model, to_file='shapes_model.png', show_shapes=True, show_layer_names=False)
model.compile(loss=dsc_loss, optimizer='adam', metrics=['mse', dsc])
# model.compile(loss='mse', optimizer='adam', metrics=['mse', dsc])
#plot_ims(model)
plt.savefig('shapes_before_training.png')
plt.show()
eps = 5
x, y = generate_data(2000)
y[y == 255] = 1
h = model.fit(x, y, validation_split=0.1, batch_size=32, epochs=eps)
#plot_ims(model)
plt.savefig('shapes_after_training.png')
plt.show()
#Plot training / validation accuracy
plt.figure()
plt.title('Shapes Segmentation')
plt.plot(range(1, eps + 1), h.history['dsc'], label='Training', color='k')
plt.plot(range(1, eps + 1), h.history['val_dsc'], label='Validation', color='r')
plt.xticks(range(1, eps + 1))
plt.xlabel('Epochs')
plt.ylabel('DICE')
plt.legend(loc='best')
plt.savefig('shapes_training.png')
plt.show()
ERROR:
In file included from C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/lib/gcc/i686-w64-mingw32/7.2.0/include/c++/math.h:36:0,
from C:\Python27\include/pyport.h:325,
Traceback (most recent call last):
from C:\Python27\include/Python.h:58,
File "C:/Users/gpapanas/PycharmProjects/shapes_segmentation_Project/shapes_segmentation.py", line 8, in <module>
from C:\Users\gpapanas\AppData\Local\Theano\compiledir_Windows-7-6.1.7601-SP1-Intel64_Family_6_Model_45_Stepping_7_GenuineIntel-2.7.14-32\lazylinker_ext\mod.cpp:1:
from keras.models import Model
C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/lib/gcc/i686-w64-mingw32/7.2.0/include/c++/cmath:1136:11: error: '::hypot' has not been declared
File "C:\Python27\lib\site-packages\keras\__init__.py", line 3, in <module>
using ::hypot;
from . import utils
^~~~~
File "C:\Python27\lib\site-packages\keras\utils\__init__.py", line 6, in <module>
from . import conv_utils
File "C:\Python27\lib\site-packages\keras\utils\conv_utils.py", line 3, in <module>
from .. import backend as K
File "C:\Python27\lib\site-packages\keras\backend\__init__.py", line 80, in <module>
from .theano_backend import *
File "C:\Python27\lib\site-packages\keras\backend\theano_backend.py", line 3, in <module>
import theano
File "C:\Python27\lib\site-packages\theano\__init__.py", line 66, in <module>
from theano.compile import (
File "C:\Python27\lib\site-packages\theano\compile\__init__.py", line 10, in <module>
from theano.compile.function_module import *
File "C:\Python27\lib\site-packages\theano\compile\function_module.py", line 21, in <module>
import theano.compile.mode
File "C:\Python27\lib\site-packages\theano\compile\mode.py", line 10, in <module>
import theano.gof.vm
File "C:\Python27\lib\site-packages\theano\gof\vm.py", line 662, in <module>
from . import lazylinker_c
File "C:\Python27\lib\site-packages\theano\gof\lazylinker_c.py", line 127, in <module>
preargs=args)
File "C:\Python27\lib\site-packages\theano\gof\cmodule.py", line 2316, in compile_str
(status, compile_stderr.replace('\n', '. ')))
Exception: Compilation failed (return status=1): In file included from C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/lib/gcc/i686-w64-mingw32/7.2.0/include/c++/math.h:36:0,. from C:\Python27\include/pyport.h:325,. from C:\Python27\include/Python.h:58,. from C:\Users\gpapanas\AppData\Local\Theano\compiledir_Windows-7-6.1.7601-SP1-Intel64_Family_6_Model_45_Stepping_7_GenuineIntel-2.7.14-32\lazylinker_ext\mod.cpp:1:. C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/lib/gcc/i686-w64-mingw32/7.2.0/include/c++/cmath:1136:11: error: '::hypot' has not been declared. using ::hypot;. ^~~~~.
Process finished with exit code 1