Explore all Keras open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Keras

keras

Keras Release 2.9.0 RC2

autokeras

Release v1.0.18

ludwig

v0.5rc2

ludwig

v0.3.1: Datasets, cache checksum, improvements for text and visualization

tensorboardX

Release v2.4

Popular Libraries in Keras

keras

by keras-team doticonpythondoticon

star image 55007 doticonApache-2.0

Deep Learning for humans

Mask_RCNN

by matterport doticonpythondoticon

star image 20508 doticonNOASSERTION

Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow

handson-ml2

by ageron doticonjupyter notebookdoticon

star image 16775 doticonApache-2.0

A series of Jupyter notebooks that walk you through the fundamentals of Machine Learning and Deep Learning in Python using Scikit-Learn, Keras and TensorFlow 2.

nndl.github.io

by nndl doticonhtmldoticon

star image 14666 doticon

《神经网络与深度学习》 邱锡鹏著 Neural Network and Deep Learning

deeplearning_ai_books

by fengdu78 doticonhtmldoticon

star image 13585 doticon

deeplearning.ai(吴恩达老师的深度学习课程笔记及资源)

Screenshot-to-code

by emilwallner doticonhtmldoticon

star image 13334 doticonNOASSERTION

A neural network that transforms a design mock-up into a static website.

DeepCreamPy

by deeppomf doticonpythondoticon

star image 10775 doticonAGPL-3.0

Decensoring Hentai with Deep Neural Networks

autokeras

by keras-team doticonpythondoticon

star image 8417 doticonApache-2.0

AutoML library for deep learning

ludwig

by ludwig-ai doticonpythondoticon

star image 8199 doticonApache-2.0

Data-centric declarative deep learning framework

Trending New libraries in Keras

best-of-ml-python

by ml-tooling doticonpythondoticon

star image 6511 doticonCC-BY-SA-4.0

🏆 A ranked list of awesome machine learning Python libraries. Updated weekly.

model_search

by google doticonpythondoticon

star image 3082 doticonApache-2.0

PaddleX

by PaddlePaddle doticonpythondoticon

star image 2635 doticonApache-2.0

PaddlePaddle End-to-End Development Toolkit(『飞桨』深度学习全流程开发工具)

deepface

by serengil doticonpythondoticon

star image 2290 doticonMIT

A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python

determined

by determined-ai doticonpythondoticon

star image 1676 doticonApache-2.0

Determined: Deep Learning Training Platform

front-end-roadmap

by ObjTube doticonjavascriptdoticon

star image 1398 doticonMIT

Tell you how to learn front end development ~

keras-ncp

by mlech26l doticonpythondoticon

star image 859 doticonApache-2.0

Code repository of the paper Neural circuit policies enabling auditable autonomy published in Nature Machine Intelligence

yolo-tf2

by schissmantics doticonpythondoticon

star image 730 doticonMIT

yolo(all versions) implementation in keras and tensorflow 2.x

yolo-tf2

by emadboctorx doticonpythondoticon

star image 703 doticonMIT

yolo(all versions) implementation in keras and tensorflow 2.5

Top Authors in Keras

1

titu1994

44 Libraries

star icon7143

2

PacktPublishing

29 Libraries

star icon2457

3

Tony607

27 Libraries

star icon1127

4

CyberZHG

21 Libraries

star icon1763

5

bojone

19 Libraries

star icon6432

6

chen0040

18 Libraries

star icon1329

7

bubbliiiing

16 Libraries

star icon1816

8

philipperemy

15 Libraries

star icon5868

9

microsoft

15 Libraries

star icon6508

10

IBM

12 Libraries

star icon428

1

44 Libraries

star icon7143

2

29 Libraries

star icon2457

3

27 Libraries

star icon1127

4

21 Libraries

star icon1763

5

19 Libraries

star icon6432

6

18 Libraries

star icon1329

7

16 Libraries

star icon1816

8

15 Libraries

star icon5868

9

15 Libraries

star icon6508

10

12 Libraries

star icon428

Trending Kits in Keras

No Trending Kits are available at this moment for Keras

Trending Discussions on Keras

When recognizing hand gesture classes, I always get the same class in Keras

WebSocket not working when trying to send generated answer by keras

Tensorflow setup on RStudio/ R | CentOS

Saving model on Tensorflow 2.7.0 with data augmentation layer

OpenVino converted model not returning same score values as original model (Sigmoid)

Is it possible to use a collection of hyperspectral 1x1 pixels in a CNN model purposed for more conventional datasets (CIFAR-10/MNIST)?

ImportError: cannot import name 'BatchNormalization' from 'keras.layers.normalization'

Unable to (manually) load cifar10 dataset

AssertionError: Tried to export a function which references untracked resource

Tensorflow - Multi-GPU doesn’t work for model(inputs) nor when computing the gradients

QUESTION

When recognizing hand gesture classes, I always get the same class in Keras

Asked 2022-Feb-22 at 13:49

When recognizing hand gesture classes, I always get the same class, although I tried changing the parameters and even passed the data without normalization:

1df_train = pd.read_csv('train_dataset.csv')
2df_train = df_train.drop(columns=['Unnamed: 0'], axis=1)
3df_train = df_train.fillna(0)
4
5x_train = df_train.drop(['y'], axis=1)
6y_train = df_train['y']
7
8x_train = x_train / 310
9
10model = keras.models.Sequential([Dense(32, input_shape=(42,), activation='relu'),
11                                Dense(64, activation='relu'),
12                                Dense(6, activation='softmax')])
13
14model.compile(optimizer='adam',
15             loss='categorical_crossentropy',
16             metrics=['accuracy'])
17
18model.fit(x_train, y_train_cat, batch_size=16, epochs=9, validation_split=0.2)
19
20model.save("gestures_model.h5")
21

Here is a main code:

1df_train = pd.read_csv('train_dataset.csv')
2df_train = df_train.drop(columns=['Unnamed: 0'], axis=1)
3df_train = df_train.fillna(0)
4
5x_train = df_train.drop(['y'], axis=1)
6y_train = df_train['y']
7
8x_train = x_train / 310
9
10model = keras.models.Sequential([Dense(32, input_shape=(42,), activation='relu'),
11                                Dense(64, activation='relu'),
12                                Dense(6, activation='softmax')])
13
14model.compile(optimizer='adam',
15             loss='categorical_crossentropy',
16             metrics=['accuracy'])
17
18model.fit(x_train, y_train_cat, batch_size=16, epochs=9, validation_split=0.2)
19
20model.save("gestures_model.h5")
21REV_CLASS_MAP = {
22    0: "up",
23    1: "down",
24    2: "right",
25    3: "left",
26    4: "forward",
27    5: "back"
28}
29
30def mapper(val):
31    return REV_CLASS_MAP[val]
32
33if len(data[data.index(new_row)]) > 0:
34    df = pd.DataFrame(data, columns=columns)
35    df = df.fillna(0)
36    df = df / 310
37    pred = model.predict(df)
38    move_code = np.argmax(pred[0])
39    user_move_name = mapper(move_code)
40    print(user_move_name)
41

Here is an example of input data:

1df_train = pd.read_csv('train_dataset.csv')
2df_train = df_train.drop(columns=['Unnamed: 0'], axis=1)
3df_train = df_train.fillna(0)
4
5x_train = df_train.drop(['y'], axis=1)
6y_train = df_train['y']
7
8x_train = x_train / 310
9
10model = keras.models.Sequential([Dense(32, input_shape=(42,), activation='relu'),
11                                Dense(64, activation='relu'),
12                                Dense(6, activation='softmax')])
13
14model.compile(optimizer='adam',
15             loss='categorical_crossentropy',
16             metrics=['accuracy'])
17
18model.fit(x_train, y_train_cat, batch_size=16, epochs=9, validation_split=0.2)
19
20model.save("gestures_model.h5")
21REV_CLASS_MAP = {
22    0: "up",
23    1: "down",
24    2: "right",
25    3: "left",
26    4: "forward",
27    5: "back"
28}
29
30def mapper(val):
31    return REV_CLASS_MAP[val]
32
33if len(data[data.index(new_row)]) > 0:
34    df = pd.DataFrame(data, columns=columns)
35    df = df.fillna(0)
36    df = df / 310
37    pred = model.predict(df)
38    move_code = np.argmax(pred[0])
39    user_move_name = mapper(move_code)
40    print(user_move_name)
4156,172,72,169,88,155,100,144,111,139,78,120,81,94,82,77,82,62,66,120,62,104,62,124,64,136,54,122,50,110,52,130,55,139,43,126,40,114,42,129,45,137,0
42

What am I doing wrong and how to fix it? I noticed that in my data there are rows in which there is only one number. Could this be the cause of my problem? ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ P.S I am new to neural networks and keras.

New

Here is df_train before processing:

1df_train = pd.read_csv('train_dataset.csv')
2df_train = df_train.drop(columns=['Unnamed: 0'], axis=1)
3df_train = df_train.fillna(0)
4
5x_train = df_train.drop(['y'], axis=1)
6y_train = df_train['y']
7
8x_train = x_train / 310
9
10model = keras.models.Sequential([Dense(32, input_shape=(42,), activation='relu'),
11                                Dense(64, activation='relu'),
12                                Dense(6, activation='softmax')])
13
14model.compile(optimizer='adam',
15             loss='categorical_crossentropy',
16             metrics=['accuracy'])
17
18model.fit(x_train, y_train_cat, batch_size=16, epochs=9, validation_split=0.2)
19
20model.save("gestures_model.h5")
21REV_CLASS_MAP = {
22    0: "up",
23    1: "down",
24    2: "right",
25    3: "left",
26    4: "forward",
27    5: "back"
28}
29
30def mapper(val):
31    return REV_CLASS_MAP[val]
32
33if len(data[data.index(new_row)]) > 0:
34    df = pd.DataFrame(data, columns=columns)
35    df = df.fillna(0)
36    df = df / 310
37    pred = model.predict(df)
38    move_code = np.argmax(pred[0])
39    user_move_name = mapper(move_code)
40    print(user_move_name)
4156,172,72,169,88,155,100,144,111,139,78,120,81,94,82,77,82,62,66,120,62,104,62,124,64,136,54,122,50,110,52,130,55,139,43,126,40,114,42,129,45,137,0
42,x11,x21,x12,x22,x13,x23,x14,x24,x15,x25,x16,x26,x17,x27,x18,x28,x19,x29,x110,x210,x111,x211,x112,x212,x113,x213,114,214,115,x215,x116,x216,x117,x217,x118,x218,x119,x219,x120,x220,x121,x221,y
4356,172,72,169,88,155,100,144,111,139,78,120,81,94,82,77,82,62,66,120,62,104,62,124,64,136,54,122,50,110,52,130,55,139,43,126,40,114,42,129,45,137,0
44...
4584,166,96,158,108,143,108,131,101,127,87,145,87,128,90,118,94,111,74,147,76,119,81,114,84,115,64,148,66,120,72,119,74,124,56,148,57,124,61,124,63,129,5
46

Here is df_train after processing:

1df_train = pd.read_csv('train_dataset.csv')
2df_train = df_train.drop(columns=['Unnamed: 0'], axis=1)
3df_train = df_train.fillna(0)
4
5x_train = df_train.drop(['y'], axis=1)
6y_train = df_train['y']
7
8x_train = x_train / 310
9
10model = keras.models.Sequential([Dense(32, input_shape=(42,), activation='relu'),
11                                Dense(64, activation='relu'),
12                                Dense(6, activation='softmax')])
13
14model.compile(optimizer='adam',
15             loss='categorical_crossentropy',
16             metrics=['accuracy'])
17
18model.fit(x_train, y_train_cat, batch_size=16, epochs=9, validation_split=0.2)
19
20model.save("gestures_model.h5")
21REV_CLASS_MAP = {
22    0: "up",
23    1: "down",
24    2: "right",
25    3: "left",
26    4: "forward",
27    5: "back"
28}
29
30def mapper(val):
31    return REV_CLASS_MAP[val]
32
33if len(data[data.index(new_row)]) > 0:
34    df = pd.DataFrame(data, columns=columns)
35    df = df.fillna(0)
36    df = df / 310
37    pred = model.predict(df)
38    move_code = np.argmax(pred[0])
39    user_move_name = mapper(move_code)
40    print(user_move_name)
4156,172,72,169,88,155,100,144,111,139,78,120,81,94,82,77,82,62,66,120,62,104,62,124,64,136,54,122,50,110,52,130,55,139,43,126,40,114,42,129,45,137,0
42,x11,x21,x12,x22,x13,x23,x14,x24,x15,x25,x16,x26,x17,x27,x18,x28,x19,x29,x110,x210,x111,x211,x112,x212,x113,x213,114,214,115,x215,x116,x216,x117,x217,x118,x218,x119,x219,x120,x220,x121,x221,y
4356,172,72,169,88,155,100,144,111,139,78,120,81,94,82,77,82,62,66,120,62,104,62,124,64,136,54,122,50,110,52,130,55,139,43,126,40,114,42,129,45,137,0
44...
4584,166,96,158,108,143,108,131,101,127,87,145,87,128,90,118,94,111,74,147,76,119,81,114,84,115,64,148,66,120,72,119,74,124,56,148,57,124,61,124,63,129,5
46     x11  x21  x12  x22  x13  x23  x14  ...  x119  x219  x120  x220  x121  x221    y
470     56  175   73  168   88  155  101  ...    42   113    44   130    47   138  0.0
481    172   72  169   88  155  100  144  ...   114    42   129    45   137     0  0.0
492    172   72  169   88  155  100  144  ...   114    42   129    45   137     0  0.0
503    174   74  167   89  155  101  144  ...   115    44   130    46   137     0  0.0
514    174   74  169   90  155  101  144  ...   114    44   128    46   136     0  0.0
52..   ...  ...  ...  ...  ...  ...  ...  ...   ...   ...   ...   ...   ...   ...  ...
53843  166   96  158  108  143  108  131  ...   124    61   124    63   129     5  0.0
54844  166   94  158  105  145  104  132  ...   128    58   130    59   134     5  0.0
55845  164   90  155  101  141  100  129  ...   126    55   129    57   134     5  0.0
56846  158   88  152   99  140   96  128  ...   142    54   150    58   146     5  0.0
57847  158   88  152   99  140   96  128  ...   142    54   150    58   146     5  0.0
58

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

ANSWER

Answered 2022-Feb-17 at 18:48

All rows need the same data size, of course some values can be empty in csv.

1df_train = pd.read_csv('train_dataset.csv')
2df_train = df_train.drop(columns=['Unnamed: 0'], axis=1)
3df_train = df_train.fillna(0)
4
5x_train = df_train.drop(['y'], axis=1)
6y_train = df_train['y']
7
8x_train = x_train / 310
9
10model = keras.models.Sequential([Dense(32, input_shape=(42,), activation='relu'),
11                                Dense(64, activation='relu'),
12                                Dense(6, activation='softmax')])
13
14model.compile(optimizer='adam',
15             loss='categorical_crossentropy',
16             metrics=['accuracy'])
17
18model.fit(x_train, y_train_cat, batch_size=16, epochs=9, validation_split=0.2)
19
20model.save("gestures_model.h5")
21REV_CLASS_MAP = {
22    0: "up",
23    1: "down",
24    2: "right",
25    3: "left",
26    4: "forward",
27    5: "back"
28}
29
30def mapper(val):
31    return REV_CLASS_MAP[val]
32
33if len(data[data.index(new_row)]) > 0:
34    df = pd.DataFrame(data, columns=columns)
35    df = df.fillna(0)
36    df = df / 310
37    pred = model.predict(df)
38    move_code = np.argmax(pred[0])
39    user_move_name = mapper(move_code)
40    print(user_move_name)
4156,172,72,169,88,155,100,144,111,139,78,120,81,94,82,77,82,62,66,120,62,104,62,124,64,136,54,122,50,110,52,130,55,139,43,126,40,114,42,129,45,137,0
42,x11,x21,x12,x22,x13,x23,x14,x24,x15,x25,x16,x26,x17,x27,x18,x28,x19,x29,x110,x210,x111,x211,x112,x212,x113,x213,114,214,115,x215,x116,x216,x117,x217,x118,x218,x119,x219,x120,x220,x121,x221,y
4356,172,72,169,88,155,100,144,111,139,78,120,81,94,82,77,82,62,66,120,62,104,62,124,64,136,54,122,50,110,52,130,55,139,43,126,40,114,42,129,45,137,0
44...
4584,166,96,158,108,143,108,131,101,127,87,145,87,128,90,118,94,111,74,147,76,119,81,114,84,115,64,148,66,120,72,119,74,124,56,148,57,124,61,124,63,129,5
46     x11  x21  x12  x22  x13  x23  x14  ...  x119  x219  x120  x220  x121  x221    y
470     56  175   73  168   88  155  101  ...    42   113    44   130    47   138  0.0
481    172   72  169   88  155  100  144  ...   114    42   129    45   137     0  0.0
492    172   72  169   88  155  100  144  ...   114    42   129    45   137     0  0.0
503    174   74  167   89  155  101  144  ...   115    44   130    46   137     0  0.0
514    174   74  169   90  155  101  144  ...   114    44   128    46   136     0  0.0
52..   ...  ...  ...  ...  ...  ...  ...  ...   ...   ...   ...   ...   ...   ...  ...
53843  166   96  158  108  143  108  131  ...   124    61   124    63   129     5  0.0
54844  166   94  158  105  145  104  132  ...   128    58   130    59   134     5  0.0
55845  164   90  155  101  141  100  129  ...   126    55   129    57   134     5  0.0
56846  158   88  152   99  140   96  128  ...   142    54   150    58   146     5  0.0
57847  158   88  152   99  140   96  128  ...   142    54   150    58   146     5  0.0
58feature1, feature2, feature3,y
59aaa,bbb,3.0,2.0
60bbb, ,4.1, 3.1
61

You need to impute empty values by using for example most frequent value for categorical values or median for numerical values. Predicted value cant be empty

Source https://stackoverflow.com/questions/71163462

QUESTION

WebSocket not working when trying to send generated answer by keras

Asked 2022-Feb-17 at 12:52

I am implementing a simple chatbot using keras and WebSockets. I now have a model that can make a prediction about the user input and send the according answer.

When I do it through command line it works fine, however when I try to send the answer through my WebSocket, the WebSocket doesn't even start anymore.

Here is my working WebSocket code:

1@sock.route('/api')
2def echo(sock):
3    while True:
4        # get user input from browser
5        user_input = sock.receive()
6        # print user input on console
7        print(user_input)
8        # read answer from console
9        response = input()
10        # send response to browser
11        sock.send(response)
12

Here is my code to communicate with the keras model on command line:

1@sock.route('/api')
2def echo(sock):
3    while True:
4        # get user input from browser
5        user_input = sock.receive()
6        # print user input on console
7        print(user_input)
8        # read answer from console
9        response = input()
10        # send response to browser
11        sock.send(response)
12while True:
13    question = input("")
14    ints = predict(question)
15    answer = response(ints, json_data)
16    print(answer)
17

Used methods are those:

1@sock.route('/api')
2def echo(sock):
3    while True:
4        # get user input from browser
5        user_input = sock.receive()
6        # print user input on console
7        print(user_input)
8        # read answer from console
9        response = input()
10        # send response to browser
11        sock.send(response)
12while True:
13    question = input("")
14    ints = predict(question)
15    answer = response(ints, json_data)
16    print(answer)
17def predict(sentence):
18    bag_of_words = convert_sentence_in_bag_of_words(sentence)
19    # pass bag as list and get index 0
20    prediction = model.predict(np.array([bag_of_words]))[0]
21    ERROR_THRESHOLD = 0.25
22    accepted_results = [[tag, probability] for tag, probability in enumerate(prediction) if probability > ERROR_THRESHOLD]
23
24    accepted_results.sort(key=lambda x: x[1], reverse=True)
25
26    output = []
27    for accepted_result in accepted_results:
28        output.append({'intent': classes[accepted_result[0]], 'probability': str(accepted_result[1])})
29        print(output)
30    return output
31
32
33def response(intents, json):
34    tag = intents[0]['intent']
35    intents_as_list = json['intents']
36    for i in intents_as_list:
37        if i['tag'] == tag:
38            res = random.choice(i['responses'])
39            break
40    return res
41

So when I start the WebSocket with the working code I get this output:

1@sock.route('/api')
2def echo(sock):
3    while True:
4        # get user input from browser
5        user_input = sock.receive()
6        # print user input on console
7        print(user_input)
8        # read answer from console
9        response = input()
10        # send response to browser
11        sock.send(response)
12while True:
13    question = input("")
14    ints = predict(question)
15    answer = response(ints, json_data)
16    print(answer)
17def predict(sentence):
18    bag_of_words = convert_sentence_in_bag_of_words(sentence)
19    # pass bag as list and get index 0
20    prediction = model.predict(np.array([bag_of_words]))[0]
21    ERROR_THRESHOLD = 0.25
22    accepted_results = [[tag, probability] for tag, probability in enumerate(prediction) if probability > ERROR_THRESHOLD]
23
24    accepted_results.sort(key=lambda x: x[1], reverse=True)
25
26    output = []
27    for accepted_result in accepted_results:
28        output.append({'intent': classes[accepted_result[0]], 'probability': str(accepted_result[1])})
29        print(output)
30    return output
31
32
33def response(intents, json):
34    tag = intents[0]['intent']
35    intents_as_list = json['intents']
36    for i in intents_as_list:
37        if i['tag'] == tag:
38            res = random.choice(i['responses'])
39            break
40    return res
41 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
42 * Restarting with stat
43 * Serving Flask app 'server' (lazy loading)
44 * Environment: production
45   WARNING: This is a development server. Do not use it in a production deployment.
46   Use a production WSGI server instead.
47 * Debug mode: on
48

But as soon as I have anything of my model in the server.py class I get this output:

1@sock.route('/api')
2def echo(sock):
3    while True:
4        # get user input from browser
5        user_input = sock.receive()
6        # print user input on console
7        print(user_input)
8        # read answer from console
9        response = input()
10        # send response to browser
11        sock.send(response)
12while True:
13    question = input("")
14    ints = predict(question)
15    answer = response(ints, json_data)
16    print(answer)
17def predict(sentence):
18    bag_of_words = convert_sentence_in_bag_of_words(sentence)
19    # pass bag as list and get index 0
20    prediction = model.predict(np.array([bag_of_words]))[0]
21    ERROR_THRESHOLD = 0.25
22    accepted_results = [[tag, probability] for tag, probability in enumerate(prediction) if probability > ERROR_THRESHOLD]
23
24    accepted_results.sort(key=lambda x: x[1], reverse=True)
25
26    output = []
27    for accepted_result in accepted_results:
28        output.append({'intent': classes[accepted_result[0]], 'probability': str(accepted_result[1])})
29        print(output)
30    return output
31
32
33def response(intents, json):
34    tag = intents[0]['intent']
35    intents_as_list = json['intents']
36    for i in intents_as_list:
37        if i['tag'] == tag:
38            res = random.choice(i['responses'])
39            break
40    return res
41 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
42 * Restarting with stat
43 * Serving Flask app 'server' (lazy loading)
44 * Environment: production
45   WARNING: This is a development server. Do not use it in a production deployment.
46   Use a production WSGI server instead.
47 * Debug mode: on
482022-02-13 11:31:38.887640: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
492022-02-13 11:31:38.887734: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
50Metal device set to: Apple M1
51
52systemMemory: 16.00 GB
53maxCacheSize: 5.33 GB
54

It is enough when I just have an import at the top like this: from chatty import response, predict - even though they are unused.

ANSWER

Answered 2022-Feb-16 at 19:53

There is no problem with your websocket route. Could you please share how you are triggering this route? Websocket is a different protocol and I'm suspecting that you are using a HTTP client to test websocket. For example in Postman:

Postman New Screen

HTTP requests are different than websocket requests. So, you should use appropriate client to test websocket.

Source https://stackoverflow.com/questions/71099818

QUESTION

Tensorflow setup on RStudio/ R | CentOS

Asked 2022-Feb-11 at 09:36

For the last 5 days, I am trying to make Keras/Tensorflow packages work in R. I am using RStudio for installation and have used conda, miniconda, virtualenv but it crashes each time in the end. Installing a library should not be a nightmare especially when we are talking about R (one of the best statistical languages) and TensorFlow (one of the best deep learning libraries). Can someone share a reliable way to install Keras/Tensorflow on CentOS 7?

Following are the steps I am using to install tensorflow in RStudio.

Since RStudio simply crashes each time I run tensorflow::tf_config() I have no way to check what is going wrong.

enter image description here

1devtools::install_github("rstudio/reticulate")
2devtools::install_github("rstudio/keras") # This package also installs tensorflow
3library(reticulate)
4reticulate::install_miniconda()
5reticulate::use_miniconda("r-reticulate")
6library(tensorflow)
7tensorflow::tf_config() **# Crashes at this point**
8
9sessionInfo()
10
11
12R version 3.6.0 (2019-04-26)
13Platform: x86_64-redhat-linux-gnu (64-bit)
14Running under: CentOS Linux 7 (Core)
15
16Matrix products: default
17BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
18
19locale:
20 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
21 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
22 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
23 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
24 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
25[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
26
27attached base packages:
28[1] stats     graphics  grDevices utils     datasets  methods   base     
29
30other attached packages:
31[1] tensorflow_2.7.0.9000 keras_2.7.0.9000      reticulate_1.22-9000 
32
33loaded via a namespace (and not attached):
34 [1] Rcpp_1.0.7      lattice_0.20-45 png_0.1-7       zeallot_0.1.0  
35 [5] rappdirs_0.3.3  grid_3.6.0      R6_2.5.1        jsonlite_1.7.2 
36 [9] magrittr_2.0.1  tfruns_1.5.0    rlang_0.4.12    whisker_0.4    
37[13] Matrix_1.3-4    generics_0.1.1  tools_3.6.0     compiler_3.6.0 
38[17] base64enc_0.1-3
39
40
41

Update 1 The only way RStudio does not crash while installing tensorflow is by executing following steps -

First, I created a new virtual environment using conda

1devtools::install_github("rstudio/reticulate")
2devtools::install_github("rstudio/keras") # This package also installs tensorflow
3library(reticulate)
4reticulate::install_miniconda()
5reticulate::use_miniconda("r-reticulate")
6library(tensorflow)
7tensorflow::tf_config() **# Crashes at this point**
8
9sessionInfo()
10
11
12R version 3.6.0 (2019-04-26)
13Platform: x86_64-redhat-linux-gnu (64-bit)
14Running under: CentOS Linux 7 (Core)
15
16Matrix products: default
17BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
18
19locale:
20 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
21 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
22 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
23 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
24 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
25[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
26
27attached base packages:
28[1] stats     graphics  grDevices utils     datasets  methods   base     
29
30other attached packages:
31[1] tensorflow_2.7.0.9000 keras_2.7.0.9000      reticulate_1.22-9000 
32
33loaded via a namespace (and not attached):
34 [1] Rcpp_1.0.7      lattice_0.20-45 png_0.1-7       zeallot_0.1.0  
35 [5] rappdirs_0.3.3  grid_3.6.0      R6_2.5.1        jsonlite_1.7.2 
36 [9] magrittr_2.0.1  tfruns_1.5.0    rlang_0.4.12    whisker_0.4    
37[13] Matrix_1.3-4    generics_0.1.1  tools_3.6.0     compiler_3.6.0 
38[17] base64enc_0.1-3
39
40
41conda create --name py38 python=3.8.0
42conda activate py38
43conda install tensorflow=2.4
44

Then from within RStudio, I installed reticulate and activated the virtual environment which I earlier created using conda

1devtools::install_github("rstudio/reticulate")
2devtools::install_github("rstudio/keras") # This package also installs tensorflow
3library(reticulate)
4reticulate::install_miniconda()
5reticulate::use_miniconda("r-reticulate")
6library(tensorflow)
7tensorflow::tf_config() **# Crashes at this point**
8
9sessionInfo()
10
11
12R version 3.6.0 (2019-04-26)
13Platform: x86_64-redhat-linux-gnu (64-bit)
14Running under: CentOS Linux 7 (Core)
15
16Matrix products: default
17BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
18
19locale:
20 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
21 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
22 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
23 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
24 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
25[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
26
27attached base packages:
28[1] stats     graphics  grDevices utils     datasets  methods   base     
29
30other attached packages:
31[1] tensorflow_2.7.0.9000 keras_2.7.0.9000      reticulate_1.22-9000 
32
33loaded via a namespace (and not attached):
34 [1] Rcpp_1.0.7      lattice_0.20-45 png_0.1-7       zeallot_0.1.0  
35 [5] rappdirs_0.3.3  grid_3.6.0      R6_2.5.1        jsonlite_1.7.2 
36 [9] magrittr_2.0.1  tfruns_1.5.0    rlang_0.4.12    whisker_0.4    
37[13] Matrix_1.3-4    generics_0.1.1  tools_3.6.0     compiler_3.6.0 
38[17] base64enc_0.1-3
39
40
41conda create --name py38 python=3.8.0
42conda activate py38
43conda install tensorflow=2.4
44devtools::install_github("rstudio/reticulate")
45library(reticulate)
46reticulate::use_condaenv("/root/.conda/envs/py38", required = TRUE)
47reticulate::use_python("/root/.conda/envs/py38/bin/python3.8", required = TRUE)
48reticulate::py_available(initialize = TRUE)
49ts <- reticulate::import("tensorflow")
50

As soon as I try to import tensorflow in RStudio, it loads the library /lib64/libstdc++.so.6 instead of /root/.conda/envs/py38/lib/libstdc++.so.6 and I get the following error -

1devtools::install_github("rstudio/reticulate")
2devtools::install_github("rstudio/keras") # This package also installs tensorflow
3library(reticulate)
4reticulate::install_miniconda()
5reticulate::use_miniconda("r-reticulate")
6library(tensorflow)
7tensorflow::tf_config() **# Crashes at this point**
8
9sessionInfo()
10
11
12R version 3.6.0 (2019-04-26)
13Platform: x86_64-redhat-linux-gnu (64-bit)
14Running under: CentOS Linux 7 (Core)
15
16Matrix products: default
17BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
18
19locale:
20 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
21 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
22 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
23 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
24 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
25[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
26
27attached base packages:
28[1] stats     graphics  grDevices utils     datasets  methods   base     
29
30other attached packages:
31[1] tensorflow_2.7.0.9000 keras_2.7.0.9000      reticulate_1.22-9000 
32
33loaded via a namespace (and not attached):
34 [1] Rcpp_1.0.7      lattice_0.20-45 png_0.1-7       zeallot_0.1.0  
35 [5] rappdirs_0.3.3  grid_3.6.0      R6_2.5.1        jsonlite_1.7.2 
36 [9] magrittr_2.0.1  tfruns_1.5.0    rlang_0.4.12    whisker_0.4    
37[13] Matrix_1.3-4    generics_0.1.1  tools_3.6.0     compiler_3.6.0 
38[17] base64enc_0.1-3
39
40
41conda create --name py38 python=3.8.0
42conda activate py38
43conda install tensorflow=2.4
44devtools::install_github("rstudio/reticulate")
45library(reticulate)
46reticulate::use_condaenv("/root/.conda/envs/py38", required = TRUE)
47reticulate::use_python("/root/.conda/envs/py38/bin/python3.8", required = TRUE)
48reticulate::py_available(initialize = TRUE)
49ts <- reticulate::import("tensorflow")
50Error in py_module_import(module, convert = convert) : 
51  ImportError: Traceback (most recent call last):
52  File "/root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
53    from tensorflow.python._pywrap_tensorflow_internal import *
54  File "/home/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/python/rpytools/loader.py", line 39, in _import_hook
55    module = _import(
56ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
57
58
59Failed to load the native TensorFlow runtime.
60
61See https://www.tensorflow.org/install/errors
62
63for some common reasons and solutions.  Include the entire stack trace
64above this error message when asking for help.
65

Here is what inside /lib64/libstdc++.so.6

1devtools::install_github("rstudio/reticulate")
2devtools::install_github("rstudio/keras") # This package also installs tensorflow
3library(reticulate)
4reticulate::install_miniconda()
5reticulate::use_miniconda("r-reticulate")
6library(tensorflow)
7tensorflow::tf_config() **# Crashes at this point**
8
9sessionInfo()
10
11
12R version 3.6.0 (2019-04-26)
13Platform: x86_64-redhat-linux-gnu (64-bit)
14Running under: CentOS Linux 7 (Core)
15
16Matrix products: default
17BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
18
19locale:
20 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
21 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
22 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
23 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
24 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
25[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
26
27attached base packages:
28[1] stats     graphics  grDevices utils     datasets  methods   base     
29
30other attached packages:
31[1] tensorflow_2.7.0.9000 keras_2.7.0.9000      reticulate_1.22-9000 
32
33loaded via a namespace (and not attached):
34 [1] Rcpp_1.0.7      lattice_0.20-45 png_0.1-7       zeallot_0.1.0  
35 [5] rappdirs_0.3.3  grid_3.6.0      R6_2.5.1        jsonlite_1.7.2 
36 [9] magrittr_2.0.1  tfruns_1.5.0    rlang_0.4.12    whisker_0.4    
37[13] Matrix_1.3-4    generics_0.1.1  tools_3.6.0     compiler_3.6.0 
38[17] base64enc_0.1-3
39
40
41conda create --name py38 python=3.8.0
42conda activate py38
43conda install tensorflow=2.4
44devtools::install_github("rstudio/reticulate")
45library(reticulate)
46reticulate::use_condaenv("/root/.conda/envs/py38", required = TRUE)
47reticulate::use_python("/root/.conda/envs/py38/bin/python3.8", required = TRUE)
48reticulate::py_available(initialize = TRUE)
49ts <- reticulate::import("tensorflow")
50Error in py_module_import(module, convert = convert) : 
51  ImportError: Traceback (most recent call last):
52  File "/root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
53    from tensorflow.python._pywrap_tensorflow_internal import *
54  File "/home/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/python/rpytools/loader.py", line 39, in _import_hook
55    module = _import(
56ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
57
58
59Failed to load the native TensorFlow runtime.
60
61See https://www.tensorflow.org/install/errors
62
63for some common reasons and solutions.  Include the entire stack trace
64above this error message when asking for help.
65> strings /lib64/libstdc++.so.6 | grep GLIBC
66
67GLIBCXX_3.4
68GLIBCXX_3.4.1
69GLIBCXX_3.4.2
70GLIBCXX_3.4.3
71GLIBCXX_3.4.4
72GLIBCXX_3.4.5
73GLIBCXX_3.4.6
74GLIBCXX_3.4.7
75GLIBCXX_3.4.8
76GLIBCXX_3.4.9
77GLIBCXX_3.4.10
78GLIBCXX_3.4.11
79GLIBCXX_3.4.12
80GLIBCXX_3.4.13
81GLIBCXX_3.4.14
82GLIBCXX_3.4.15
83GLIBCXX_3.4.16
84GLIBCXX_3.4.17
85GLIBCXX_3.4.18
86GLIBCXX_3.4.19
87GLIBC_2.3
88GLIBC_2.2.5
89GLIBC_2.14
90GLIBC_2.4
91GLIBC_2.3.2
92GLIBCXX_DEBUG_MESSAGE_LENGTH
93

To resolve the library issue, I added the path of the correct libstdc++.so.6 library having GLIBCXX_3.4.20 in RStudio.

1devtools::install_github("rstudio/reticulate")
2devtools::install_github("rstudio/keras") # This package also installs tensorflow
3library(reticulate)
4reticulate::install_miniconda()
5reticulate::use_miniconda("r-reticulate")
6library(tensorflow)
7tensorflow::tf_config() **# Crashes at this point**
8
9sessionInfo()
10
11
12R version 3.6.0 (2019-04-26)
13Platform: x86_64-redhat-linux-gnu (64-bit)
14Running under: CentOS Linux 7 (Core)
15
16Matrix products: default
17BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
18
19locale:
20 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
21 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
22 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
23 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
24 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
25[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
26
27attached base packages:
28[1] stats     graphics  grDevices utils     datasets  methods   base     
29
30other attached packages:
31[1] tensorflow_2.7.0.9000 keras_2.7.0.9000      reticulate_1.22-9000 
32
33loaded via a namespace (and not attached):
34 [1] Rcpp_1.0.7      lattice_0.20-45 png_0.1-7       zeallot_0.1.0  
35 [5] rappdirs_0.3.3  grid_3.6.0      R6_2.5.1        jsonlite_1.7.2 
36 [9] magrittr_2.0.1  tfruns_1.5.0    rlang_0.4.12    whisker_0.4    
37[13] Matrix_1.3-4    generics_0.1.1  tools_3.6.0     compiler_3.6.0 
38[17] base64enc_0.1-3
39
40
41conda create --name py38 python=3.8.0
42conda activate py38
43conda install tensorflow=2.4
44devtools::install_github("rstudio/reticulate")
45library(reticulate)
46reticulate::use_condaenv("/root/.conda/envs/py38", required = TRUE)
47reticulate::use_python("/root/.conda/envs/py38/bin/python3.8", required = TRUE)
48reticulate::py_available(initialize = TRUE)
49ts <- reticulate::import("tensorflow")
50Error in py_module_import(module, convert = convert) : 
51  ImportError: Traceback (most recent call last):
52  File "/root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
53    from tensorflow.python._pywrap_tensorflow_internal import *
54  File "/home/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/python/rpytools/loader.py", line 39, in _import_hook
55    module = _import(
56ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
57
58
59Failed to load the native TensorFlow runtime.
60
61See https://www.tensorflow.org/install/errors
62
63for some common reasons and solutions.  Include the entire stack trace
64above this error message when asking for help.
65> strings /lib64/libstdc++.so.6 | grep GLIBC
66
67GLIBCXX_3.4
68GLIBCXX_3.4.1
69GLIBCXX_3.4.2
70GLIBCXX_3.4.3
71GLIBCXX_3.4.4
72GLIBCXX_3.4.5
73GLIBCXX_3.4.6
74GLIBCXX_3.4.7
75GLIBCXX_3.4.8
76GLIBCXX_3.4.9
77GLIBCXX_3.4.10
78GLIBCXX_3.4.11
79GLIBCXX_3.4.12
80GLIBCXX_3.4.13
81GLIBCXX_3.4.14
82GLIBCXX_3.4.15
83GLIBCXX_3.4.16
84GLIBCXX_3.4.17
85GLIBCXX_3.4.18
86GLIBCXX_3.4.19
87GLIBC_2.3
88GLIBC_2.2.5
89GLIBC_2.14
90GLIBC_2.4
91GLIBC_2.3.2
92GLIBCXX_DEBUG_MESSAGE_LENGTH
93system('export LD_LIBRARY_PATH=/root/.conda/envs/py38/lib/:$LD_LIBRARY_PATH')
94

and, also

1devtools::install_github("rstudio/reticulate")
2devtools::install_github("rstudio/keras") # This package also installs tensorflow
3library(reticulate)
4reticulate::install_miniconda()
5reticulate::use_miniconda("r-reticulate")
6library(tensorflow)
7tensorflow::tf_config() **# Crashes at this point**
8
9sessionInfo()
10
11
12R version 3.6.0 (2019-04-26)
13Platform: x86_64-redhat-linux-gnu (64-bit)
14Running under: CentOS Linux 7 (Core)
15
16Matrix products: default
17BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
18
19locale:
20 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
21 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
22 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
23 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
24 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
25[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
26
27attached base packages:
28[1] stats     graphics  grDevices utils     datasets  methods   base     
29
30other attached packages:
31[1] tensorflow_2.7.0.9000 keras_2.7.0.9000      reticulate_1.22-9000 
32
33loaded via a namespace (and not attached):
34 [1] Rcpp_1.0.7      lattice_0.20-45 png_0.1-7       zeallot_0.1.0  
35 [5] rappdirs_0.3.3  grid_3.6.0      R6_2.5.1        jsonlite_1.7.2 
36 [9] magrittr_2.0.1  tfruns_1.5.0    rlang_0.4.12    whisker_0.4    
37[13] Matrix_1.3-4    generics_0.1.1  tools_3.6.0     compiler_3.6.0 
38[17] base64enc_0.1-3
39
40
41conda create --name py38 python=3.8.0
42conda activate py38
43conda install tensorflow=2.4
44devtools::install_github("rstudio/reticulate")
45library(reticulate)
46reticulate::use_condaenv("/root/.conda/envs/py38", required = TRUE)
47reticulate::use_python("/root/.conda/envs/py38/bin/python3.8", required = TRUE)
48reticulate::py_available(initialize = TRUE)
49ts <- reticulate::import("tensorflow")
50Error in py_module_import(module, convert = convert) : 
51  ImportError: Traceback (most recent call last):
52  File "/root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
53    from tensorflow.python._pywrap_tensorflow_internal import *
54  File "/home/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/python/rpytools/loader.py", line 39, in _import_hook
55    module = _import(
56ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
57
58
59Failed to load the native TensorFlow runtime.
60
61See https://www.tensorflow.org/install/errors
62
63for some common reasons and solutions.  Include the entire stack trace
64above this error message when asking for help.
65> strings /lib64/libstdc++.so.6 | grep GLIBC
66
67GLIBCXX_3.4
68GLIBCXX_3.4.1
69GLIBCXX_3.4.2
70GLIBCXX_3.4.3
71GLIBCXX_3.4.4
72GLIBCXX_3.4.5
73GLIBCXX_3.4.6
74GLIBCXX_3.4.7
75GLIBCXX_3.4.8
76GLIBCXX_3.4.9
77GLIBCXX_3.4.10
78GLIBCXX_3.4.11
79GLIBCXX_3.4.12
80GLIBCXX_3.4.13
81GLIBCXX_3.4.14
82GLIBCXX_3.4.15
83GLIBCXX_3.4.16
84GLIBCXX_3.4.17
85GLIBCXX_3.4.18
86GLIBCXX_3.4.19
87GLIBC_2.3
88GLIBC_2.2.5
89GLIBC_2.14
90GLIBC_2.4
91GLIBC_2.3.2
92GLIBCXX_DEBUG_MESSAGE_LENGTH
93system('export LD_LIBRARY_PATH=/root/.conda/envs/py38/lib/:$LD_LIBRARY_PATH')
94Sys.setenv("LD_LIBRARY_PATH" = "/root/.conda/envs/py38/lib")
95

But still I get the same error ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20'. Somehow RStudio still loads /lib64/libstdc++.so.6 first instead of /root/.conda/envs/py38/lib/libstdc++.so.6

Instead of RStudio, if I execute the above steps in the R console, then also I get the exact same error.

Update 2: A solution is posted here

ANSWER

Answered 2022-Jan-16 at 00:08

Perhaps my failed attempts will help someone else solve this problem; my approach:

  • boot up a clean CentOS 7 vm
  • install R and some dependencies
1devtools::install_github("rstudio/reticulate")
2devtools::install_github("rstudio/keras") # This package also installs tensorflow
3library(reticulate)
4reticulate::install_miniconda()
5reticulate::use_miniconda("r-reticulate")
6library(tensorflow)
7tensorflow::tf_config() **# Crashes at this point**
8
9sessionInfo()
10
11
12R version 3.6.0 (2019-04-26)
13Platform: x86_64-redhat-linux-gnu (64-bit)
14Running under: CentOS Linux 7 (Core)
15
16Matrix products: default
17BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
18
19locale:
20 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
21 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
22 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
23 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
24 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
25[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
26
27attached base packages:
28[1] stats     graphics  grDevices utils     datasets  methods   base     
29
30other attached packages:
31[1] tensorflow_2.7.0.9000 keras_2.7.0.9000      reticulate_1.22-9000 
32
33loaded via a namespace (and not attached):
34 [1] Rcpp_1.0.7      lattice_0.20-45 png_0.1-7       zeallot_0.1.0  
35 [5] rappdirs_0.3.3  grid_3.6.0      R6_2.5.1        jsonlite_1.7.2 
36 [9] magrittr_2.0.1  tfruns_1.5.0    rlang_0.4.12    whisker_0.4    
37[13] Matrix_1.3-4    generics_0.1.1  tools_3.6.0     compiler_3.6.0 
38[17] base64enc_0.1-3
39
40
41conda create --name py38 python=3.8.0
42conda activate py38
43conda install tensorflow=2.4
44devtools::install_github("rstudio/reticulate")
45library(reticulate)
46reticulate::use_condaenv("/root/.conda/envs/py38", required = TRUE)
47reticulate::use_python("/root/.conda/envs/py38/bin/python3.8", required = TRUE)
48reticulate::py_available(initialize = TRUE)
49ts <- reticulate::import("tensorflow")
50Error in py_module_import(module, convert = convert) : 
51  ImportError: Traceback (most recent call last):
52  File "/root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
53    from tensorflow.python._pywrap_tensorflow_internal import *
54  File "/home/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/python/rpytools/loader.py", line 39, in _import_hook
55    module = _import(
56ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
57
58
59Failed to load the native TensorFlow runtime.
60
61See https://www.tensorflow.org/install/errors
62
63for some common reasons and solutions.  Include the entire stack trace
64above this error message when asking for help.
65> strings /lib64/libstdc++.so.6 | grep GLIBC
66
67GLIBCXX_3.4
68GLIBCXX_3.4.1
69GLIBCXX_3.4.2
70GLIBCXX_3.4.3
71GLIBCXX_3.4.4
72GLIBCXX_3.4.5
73GLIBCXX_3.4.6
74GLIBCXX_3.4.7
75GLIBCXX_3.4.8
76GLIBCXX_3.4.9
77GLIBCXX_3.4.10
78GLIBCXX_3.4.11
79GLIBCXX_3.4.12
80GLIBCXX_3.4.13
81GLIBCXX_3.4.14
82GLIBCXX_3.4.15
83GLIBCXX_3.4.16
84GLIBCXX_3.4.17
85GLIBCXX_3.4.18
86GLIBCXX_3.4.19
87GLIBC_2.3
88GLIBC_2.2.5
89GLIBC_2.14
90GLIBC_2.4
91GLIBC_2.3.2
92GLIBCXX_DEBUG_MESSAGE_LENGTH
93system('export LD_LIBRARY_PATH=/root/.conda/envs/py38/lib/:$LD_LIBRARY_PATH')
94Sys.setenv("LD_LIBRARY_PATH" = "/root/.conda/envs/py38/lib")
95sudo yum install epel-release
96sudo yum install R
97sudo yum install libxml2-devel
98sudo yum install openssl-devel
99sudo yum install libcurl-devel
100sudo yum install libXcomposite libXcursor libXi libXtst libXrandr alsa-lib mesa-libEGL libXdamage mesa-libGL libXScrnSaver
101
  • Download and install Anaconda via linux installer script
  • Create a new conda env
1devtools::install_github("rstudio/reticulate")
2devtools::install_github("rstudio/keras") # This package also installs tensorflow
3library(reticulate)
4reticulate::install_miniconda()
5reticulate::use_miniconda("r-reticulate")
6library(tensorflow)
7tensorflow::tf_config() **# Crashes at this point**
8
9sessionInfo()
10
11
12R version 3.6.0 (2019-04-26)
13Platform: x86_64-redhat-linux-gnu (64-bit)
14Running under: CentOS Linux 7 (Core)
15
16Matrix products: default
17BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
18
19locale:
20 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
21 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
22 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
23 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
24 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
25[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
26
27attached base packages:
28[1] stats     graphics  grDevices utils     datasets  methods   base     
29
30other attached packages:
31[1] tensorflow_2.7.0.9000 keras_2.7.0.9000      reticulate_1.22-9000 
32
33loaded via a namespace (and not attached):
34 [1] Rcpp_1.0.7      lattice_0.20-45 png_0.1-7       zeallot_0.1.0  
35 [5] rappdirs_0.3.3  grid_3.6.0      R6_2.5.1        jsonlite_1.7.2 
36 [9] magrittr_2.0.1  tfruns_1.5.0    rlang_0.4.12    whisker_0.4    
37[13] Matrix_1.3-4    generics_0.1.1  tools_3.6.0     compiler_3.6.0 
38[17] base64enc_0.1-3
39
40
41conda create --name py38 python=3.8.0
42conda activate py38
43conda install tensorflow=2.4
44devtools::install_github("rstudio/reticulate")
45library(reticulate)
46reticulate::use_condaenv("/root/.conda/envs/py38", required = TRUE)
47reticulate::use_python("/root/.conda/envs/py38/bin/python3.8", required = TRUE)
48reticulate::py_available(initialize = TRUE)
49ts <- reticulate::import("tensorflow")
50Error in py_module_import(module, convert = convert) : 
51  ImportError: Traceback (most recent call last):
52  File "/root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
53    from tensorflow.python._pywrap_tensorflow_internal import *
54  File "/home/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/python/rpytools/loader.py", line 39, in _import_hook
55    module = _import(
56ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
57
58
59Failed to load the native TensorFlow runtime.
60
61See https://www.tensorflow.org/install/errors
62
63for some common reasons and solutions.  Include the entire stack trace
64above this error message when asking for help.
65> strings /lib64/libstdc++.so.6 | grep GLIBC
66
67GLIBCXX_3.4
68GLIBCXX_3.4.1
69GLIBCXX_3.4.2
70GLIBCXX_3.4.3
71GLIBCXX_3.4.4
72GLIBCXX_3.4.5
73GLIBCXX_3.4.6
74GLIBCXX_3.4.7
75GLIBCXX_3.4.8
76GLIBCXX_3.4.9
77GLIBCXX_3.4.10
78GLIBCXX_3.4.11
79GLIBCXX_3.4.12
80GLIBCXX_3.4.13
81GLIBCXX_3.4.14
82GLIBCXX_3.4.15
83GLIBCXX_3.4.16
84GLIBCXX_3.4.17
85GLIBCXX_3.4.18
86GLIBCXX_3.4.19
87GLIBC_2.3
88GLIBC_2.2.5
89GLIBC_2.14
90GLIBC_2.4
91GLIBC_2.3.2
92GLIBCXX_DEBUG_MESSAGE_LENGTH
93system('export LD_LIBRARY_PATH=/root/.conda/envs/py38/lib/:$LD_LIBRARY_PATH')
94Sys.setenv("LD_LIBRARY_PATH" = "/root/.conda/envs/py38/lib")
95sudo yum install epel-release
96sudo yum install R
97sudo yum install libxml2-devel
98sudo yum install openssl-devel
99sudo yum install libcurl-devel
100sudo yum install libXcomposite libXcursor libXi libXtst libXrandr alsa-lib mesa-libEGL libXdamage mesa-libGL libXScrnSaver
101conda init
102conda create --name tf
103conda activate tf
104conda install -c conda-forge tensorflow
105

**From within this conda env you can import tensorflow in python without error; now to access tf via R

  • install an updated gcc via devtoolset
1devtools::install_github("rstudio/reticulate")
2devtools::install_github("rstudio/keras") # This package also installs tensorflow
3library(reticulate)
4reticulate::install_miniconda()
5reticulate::use_miniconda("r-reticulate")
6library(tensorflow)
7tensorflow::tf_config() **# Crashes at this point**
8
9sessionInfo()
10
11
12R version 3.6.0 (2019-04-26)
13Platform: x86_64-redhat-linux-gnu (64-bit)
14Running under: CentOS Linux 7 (Core)
15
16Matrix products: default
17BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
18
19locale:
20 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
21 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
22 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
23 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
24 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
25[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
26
27attached base packages:
28[1] stats     graphics  grDevices utils     datasets  methods   base     
29
30other attached packages:
31[1] tensorflow_2.7.0.9000 keras_2.7.0.9000      reticulate_1.22-9000 
32
33loaded via a namespace (and not attached):
34 [1] Rcpp_1.0.7      lattice_0.20-45 png_0.1-7       zeallot_0.1.0  
35 [5] rappdirs_0.3.3  grid_3.6.0      R6_2.5.1        jsonlite_1.7.2 
36 [9] magrittr_2.0.1  tfruns_1.5.0    rlang_0.4.12    whisker_0.4    
37[13] Matrix_1.3-4    generics_0.1.1  tools_3.6.0     compiler_3.6.0 
38[17] base64enc_0.1-3
39
40
41conda create --name py38 python=3.8.0
42conda activate py38
43conda install tensorflow=2.4
44devtools::install_github("rstudio/reticulate")
45library(reticulate)
46reticulate::use_condaenv("/root/.conda/envs/py38", required = TRUE)
47reticulate::use_python("/root/.conda/envs/py38/bin/python3.8", required = TRUE)
48reticulate::py_available(initialize = TRUE)
49ts <- reticulate::import("tensorflow")
50Error in py_module_import(module, convert = convert) : 
51  ImportError: Traceback (most recent call last):
52  File "/root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
53    from tensorflow.python._pywrap_tensorflow_internal import *
54  File "/home/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/python/rpytools/loader.py", line 39, in _import_hook
55    module = _import(
56ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
57
58
59Failed to load the native TensorFlow runtime.
60
61See https://www.tensorflow.org/install/errors
62
63for some common reasons and solutions.  Include the entire stack trace
64above this error message when asking for help.
65> strings /lib64/libstdc++.so.6 | grep GLIBC
66
67GLIBCXX_3.4
68GLIBCXX_3.4.1
69GLIBCXX_3.4.2
70GLIBCXX_3.4.3
71GLIBCXX_3.4.4
72GLIBCXX_3.4.5
73GLIBCXX_3.4.6
74GLIBCXX_3.4.7
75GLIBCXX_3.4.8
76GLIBCXX_3.4.9
77GLIBCXX_3.4.10
78GLIBCXX_3.4.11
79GLIBCXX_3.4.12
80GLIBCXX_3.4.13
81GLIBCXX_3.4.14
82GLIBCXX_3.4.15
83GLIBCXX_3.4.16
84GLIBCXX_3.4.17
85GLIBCXX_3.4.18
86GLIBCXX_3.4.19
87GLIBC_2.3
88GLIBC_2.2.5
89GLIBC_2.14
90GLIBC_2.4
91GLIBC_2.3.2
92GLIBCXX_DEBUG_MESSAGE_LENGTH
93system('export LD_LIBRARY_PATH=/root/.conda/envs/py38/lib/:$LD_LIBRARY_PATH')
94Sys.setenv("LD_LIBRARY_PATH" = "/root/.conda/envs/py38/lib")
95sudo yum install epel-release
96sudo yum install R
97sudo yum install libxml2-devel
98sudo yum install openssl-devel
99sudo yum install libcurl-devel
100sudo yum install libXcomposite libXcursor libXi libXtst libXrandr alsa-lib mesa-libEGL libXdamage mesa-libGL libXScrnSaver
101conda init
102conda create --name tf
103conda activate tf
104conda install -c conda-forge tensorflow
105sudo yum install centos-release-scl
106sudo yum install devtoolset-7-gcc*
107
  • attempt to use tensorflow in R via the reticulate package
1devtools::install_github("rstudio/reticulate")
2devtools::install_github("rstudio/keras") # This package also installs tensorflow
3library(reticulate)
4reticulate::install_miniconda()
5reticulate::use_miniconda("r-reticulate")
6library(tensorflow)
7tensorflow::tf_config() **# Crashes at this point**
8
9sessionInfo()
10
11
12R version 3.6.0 (2019-04-26)
13Platform: x86_64-redhat-linux-gnu (64-bit)
14Running under: CentOS Linux 7 (Core)
15
16Matrix products: default
17BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
18
19locale:
20 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
21 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
22 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
23 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
24 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
25[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
26
27attached base packages:
28[1] stats     graphics  grDevices utils     datasets  methods   base     
29
30other attached packages:
31[1] tensorflow_2.7.0.9000 keras_2.7.0.9000      reticulate_1.22-9000 
32
33loaded via a namespace (and not attached):
34 [1] Rcpp_1.0.7      lattice_0.20-45 png_0.1-7       zeallot_0.1.0  
35 [5] rappdirs_0.3.3  grid_3.6.0      R6_2.5.1        jsonlite_1.7.2 
36 [9] magrittr_2.0.1  tfruns_1.5.0    rlang_0.4.12    whisker_0.4    
37[13] Matrix_1.3-4    generics_0.1.1  tools_3.6.0     compiler_3.6.0 
38[17] base64enc_0.1-3
39
40
41conda create --name py38 python=3.8.0
42conda activate py38
43conda install tensorflow=2.4
44devtools::install_github("rstudio/reticulate")
45library(reticulate)
46reticulate::use_condaenv("/root/.conda/envs/py38", required = TRUE)
47reticulate::use_python("/root/.conda/envs/py38/bin/python3.8", required = TRUE)
48reticulate::py_available(initialize = TRUE)
49ts <- reticulate::import("tensorflow")
50Error in py_module_import(module, convert = convert) : 
51  ImportError: Traceback (most recent call last):
52  File "/root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
53    from tensorflow.python._pywrap_tensorflow_internal import *
54  File "/home/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/python/rpytools/loader.py", line 39, in _import_hook
55    module = _import(
56ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
57
58
59Failed to load the native TensorFlow runtime.
60
61See https://www.tensorflow.org/install/errors
62
63for some common reasons and solutions.  Include the entire stack trace
64above this error message when asking for help.
65> strings /lib64/libstdc++.so.6 | grep GLIBC
66
67GLIBCXX_3.4
68GLIBCXX_3.4.1
69GLIBCXX_3.4.2
70GLIBCXX_3.4.3
71GLIBCXX_3.4.4
72GLIBCXX_3.4.5
73GLIBCXX_3.4.6
74GLIBCXX_3.4.7
75GLIBCXX_3.4.8
76GLIBCXX_3.4.9
77GLIBCXX_3.4.10
78GLIBCXX_3.4.11
79GLIBCXX_3.4.12
80GLIBCXX_3.4.13
81GLIBCXX_3.4.14
82GLIBCXX_3.4.15
83GLIBCXX_3.4.16
84GLIBCXX_3.4.17
85GLIBCXX_3.4.18
86GLIBCXX_3.4.19
87GLIBC_2.3
88GLIBC_2.2.5
89GLIBC_2.14
90GLIBC_2.4
91GLIBC_2.3.2
92GLIBCXX_DEBUG_MESSAGE_LENGTH
93system('export LD_LIBRARY_PATH=/root/.conda/envs/py38/lib/:$LD_LIBRARY_PATH')
94Sys.setenv("LD_LIBRARY_PATH" = "/root/.conda/envs/py38/lib")
95sudo yum install epel-release
96sudo yum install R
97sudo yum install libxml2-devel
98sudo yum install openssl-devel
99sudo yum install libcurl-devel
100sudo yum install libXcomposite libXcursor libXi libXtst libXrandr alsa-lib mesa-libEGL libXdamage mesa-libGL libXScrnSaver
101conda init
102conda create --name tf
103conda activate tf
104conda install -c conda-forge tensorflow
105sudo yum install centos-release-scl
106sudo yum install devtoolset-7-gcc*
107scl enable devtoolset-7 R
108install.packages("remotes")
109remotes::install_github('rstudio/reticulate')
110reticulate::use_condaenv("tf", conda = "~/anaconda3/bin/conda")
111reticulate::repl_python()
112# This works as expected but the command "import tensorflow" crashes R
113# Error: *** caught segfault *** address 0xf8, cause 'memory not mapped'
114
115# Also tried:
116install.packages("devtools")
117devtools::install_github('rstudio/tensorflow')
118devtools::install_github('rstudio/keras')
119library(tensorflow)
120install_tensorflow() # "successful"
121tensorflow::tf_config()
122# Error: *** caught segfault *** address 0xf8, cause 'memory not mapped'
123
  • try older versions of tensorflow/keras
1devtools::install_github("rstudio/reticulate")
2devtools::install_github("rstudio/keras") # This package also installs tensorflow
3library(reticulate)
4reticulate::install_miniconda()
5reticulate::use_miniconda("r-reticulate")
6library(tensorflow)
7tensorflow::tf_config() **# Crashes at this point**
8
9sessionInfo()
10
11
12R version 3.6.0 (2019-04-26)
13Platform: x86_64-redhat-linux-gnu (64-bit)
14Running under: CentOS Linux 7 (Core)
15
16Matrix products: default
17BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
18
19locale:
20 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
21 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
22 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
23 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
24 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
25[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
26
27attached base packages:
28[1] stats     graphics  grDevices utils     datasets  methods   base     
29
30other attached packages:
31[1] tensorflow_2.7.0.9000 keras_2.7.0.9000      reticulate_1.22-9000 
32
33loaded via a namespace (and not attached):
34 [1] Rcpp_1.0.7      lattice_0.20-45 png_0.1-7       zeallot_0.1.0  
35 [5] rappdirs_0.3.3  grid_3.6.0      R6_2.5.1        jsonlite_1.7.2 
36 [9] magrittr_2.0.1  tfruns_1.5.0    rlang_0.4.12    whisker_0.4    
37[13] Matrix_1.3-4    generics_0.1.1  tools_3.6.0     compiler_3.6.0 
38[17] base64enc_0.1-3
39
40
41conda create --name py38 python=3.8.0
42conda activate py38
43conda install tensorflow=2.4
44devtools::install_github("rstudio/reticulate")
45library(reticulate)
46reticulate::use_condaenv("/root/.conda/envs/py38", required = TRUE)
47reticulate::use_python("/root/.conda/envs/py38/bin/python3.8", required = TRUE)
48reticulate::py_available(initialize = TRUE)
49ts <- reticulate::import("tensorflow")
50Error in py_module_import(module, convert = convert) : 
51  ImportError: Traceback (most recent call last):
52  File "/root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
53    from tensorflow.python._pywrap_tensorflow_internal import *
54  File "/home/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/python/rpytools/loader.py", line 39, in _import_hook
55    module = _import(
56ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
57
58
59Failed to load the native TensorFlow runtime.
60
61See https://www.tensorflow.org/install/errors
62
63for some common reasons and solutions.  Include the entire stack trace
64above this error message when asking for help.
65> strings /lib64/libstdc++.so.6 | grep GLIBC
66
67GLIBCXX_3.4
68GLIBCXX_3.4.1
69GLIBCXX_3.4.2
70GLIBCXX_3.4.3
71GLIBCXX_3.4.4
72GLIBCXX_3.4.5
73GLIBCXX_3.4.6
74GLIBCXX_3.4.7
75GLIBCXX_3.4.8
76GLIBCXX_3.4.9
77GLIBCXX_3.4.10
78GLIBCXX_3.4.11
79GLIBCXX_3.4.12
80GLIBCXX_3.4.13
81GLIBCXX_3.4.14
82GLIBCXX_3.4.15
83GLIBCXX_3.4.16
84GLIBCXX_3.4.17
85GLIBCXX_3.4.18
86GLIBCXX_3.4.19
87GLIBC_2.3
88GLIBC_2.2.5
89GLIBC_2.14
90GLIBC_2.4
91GLIBC_2.3.2
92GLIBCXX_DEBUG_MESSAGE_LENGTH
93system('export LD_LIBRARY_PATH=/root/.conda/envs/py38/lib/:$LD_LIBRARY_PATH')
94Sys.setenv("LD_LIBRARY_PATH" = "/root/.conda/envs/py38/lib")
95sudo yum install epel-release
96sudo yum install R
97sudo yum install libxml2-devel
98sudo yum install openssl-devel
99sudo yum install libcurl-devel
100sudo yum install libXcomposite libXcursor libXi libXtst libXrandr alsa-lib mesa-libEGL libXdamage mesa-libGL libXScrnSaver
101conda init
102conda create --name tf
103conda activate tf
104conda install -c conda-forge tensorflow
105sudo yum install centos-release-scl
106sudo yum install devtoolset-7-gcc*
107scl enable devtoolset-7 R
108install.packages("remotes")
109remotes::install_github('rstudio/reticulate')
110reticulate::use_condaenv("tf", conda = "~/anaconda3/bin/conda")
111reticulate::repl_python()
112# This works as expected but the command "import tensorflow" crashes R
113# Error: *** caught segfault *** address 0xf8, cause 'memory not mapped'
114
115# Also tried:
116install.packages("devtools")
117devtools::install_github('rstudio/tensorflow')
118devtools::install_github('rstudio/keras')
119library(tensorflow)
120install_tensorflow() # "successful"
121tensorflow::tf_config()
122# Error: *** caught segfault *** address 0xf8, cause 'memory not mapped'
123devtools::install_github('rstudio/tensorflow@v2.4.0')
124devtools::install_github('rstudio/keras@v2.4.0')
125library(tensorflow)
126tf_config()
127# Error: *** caught segfault *** address 0xf8, cause 'memory not mapped'
128
  • Try an updated version of R (v4.0)
1devtools::install_github("rstudio/reticulate")
2devtools::install_github("rstudio/keras") # This package also installs tensorflow
3library(reticulate)
4reticulate::install_miniconda()
5reticulate::use_miniconda("r-reticulate")
6library(tensorflow)
7tensorflow::tf_config() **# Crashes at this point**
8
9sessionInfo()
10
11
12R version 3.6.0 (2019-04-26)
13Platform: x86_64-redhat-linux-gnu (64-bit)
14Running under: CentOS Linux 7 (Core)
15
16Matrix products: default
17BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
18
19locale:
20 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
21 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
22 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
23 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
24 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
25[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
26
27attached base packages:
28[1] stats     graphics  grDevices utils     datasets  methods   base     
29
30other attached packages:
31[1] tensorflow_2.7.0.9000 keras_2.7.0.9000      reticulate_1.22-9000 
32
33loaded via a namespace (and not attached):
34 [1] Rcpp_1.0.7      lattice_0.20-45 png_0.1-7       zeallot_0.1.0  
35 [5] rappdirs_0.3.3  grid_3.6.0      R6_2.5.1        jsonlite_1.7.2 
36 [9] magrittr_2.0.1  tfruns_1.5.0    rlang_0.4.12    whisker_0.4    
37[13] Matrix_1.3-4    generics_0.1.1  tools_3.6.0     compiler_3.6.0 
38[17] base64enc_0.1-3
39
40
41conda create --name py38 python=3.8.0
42conda activate py38
43conda install tensorflow=2.4
44devtools::install_github("rstudio/reticulate")
45library(reticulate)
46reticulate::use_condaenv("/root/.conda/envs/py38", required = TRUE)
47reticulate::use_python("/root/.conda/envs/py38/bin/python3.8", required = TRUE)
48reticulate::py_available(initialize = TRUE)
49ts <- reticulate::import("tensorflow")
50Error in py_module_import(module, convert = convert) : 
51  ImportError: Traceback (most recent call last):
52  File "/root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
53    from tensorflow.python._pywrap_tensorflow_internal import *
54  File "/home/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/python/rpytools/loader.py", line 39, in _import_hook
55    module = _import(
56ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
57
58
59Failed to load the native TensorFlow runtime.
60
61See https://www.tensorflow.org/install/errors
62
63for some common reasons and solutions.  Include the entire stack trace
64above this error message when asking for help.
65> strings /lib64/libstdc++.so.6 | grep GLIBC
66
67GLIBCXX_3.4
68GLIBCXX_3.4.1
69GLIBCXX_3.4.2
70GLIBCXX_3.4.3
71GLIBCXX_3.4.4
72GLIBCXX_3.4.5
73GLIBCXX_3.4.6
74GLIBCXX_3.4.7
75GLIBCXX_3.4.8
76GLIBCXX_3.4.9
77GLIBCXX_3.4.10
78GLIBCXX_3.4.11
79GLIBCXX_3.4.12
80GLIBCXX_3.4.13
81GLIBCXX_3.4.14
82GLIBCXX_3.4.15
83GLIBCXX_3.4.16
84GLIBCXX_3.4.17
85GLIBCXX_3.4.18
86GLIBCXX_3.4.19
87GLIBC_2.3
88GLIBC_2.2.5
89GLIBC_2.14
90GLIBC_2.4
91GLIBC_2.3.2
92GLIBCXX_DEBUG_MESSAGE_LENGTH
93system('export LD_LIBRARY_PATH=/root/.conda/envs/py38/lib/:$LD_LIBRARY_PATH')
94Sys.setenv("LD_LIBRARY_PATH" = "/root/.conda/envs/py38/lib")
95sudo yum install epel-release
96sudo yum install R
97sudo yum install libxml2-devel
98sudo yum install openssl-devel
99sudo yum install libcurl-devel
100sudo yum install libXcomposite libXcursor libXi libXtst libXrandr alsa-lib mesa-libEGL libXdamage mesa-libGL libXScrnSaver
101conda init
102conda create --name tf
103conda activate tf
104conda install -c conda-forge tensorflow
105sudo yum install centos-release-scl
106sudo yum install devtoolset-7-gcc*
107scl enable devtoolset-7 R
108install.packages("remotes")
109remotes::install_github('rstudio/reticulate')
110reticulate::use_condaenv("tf", conda = "~/anaconda3/bin/conda")
111reticulate::repl_python()
112# This works as expected but the command "import tensorflow" crashes R
113# Error: *** caught segfault *** address 0xf8, cause 'memory not mapped'
114
115# Also tried:
116install.packages("devtools")
117devtools::install_github('rstudio/tensorflow')
118devtools::install_github('rstudio/keras')
119library(tensorflow)
120install_tensorflow() # "successful"
121tensorflow::tf_config()
122# Error: *** caught segfault *** address 0xf8, cause 'memory not mapped'
123devtools::install_github('rstudio/tensorflow@v2.4.0')
124devtools::install_github('rstudio/keras@v2.4.0')
125library(tensorflow)
126tf_config()
127# Error: *** caught segfault *** address 0xf8, cause 'memory not mapped'
128# deactivate conda
129sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 
130export R_VERSION=4.0.0
131curl -O https://cdn.rstudio.com/r/centos-7/pkgs/R-${R_VERSION}-1-1.x86_64.rpm
132sudo yum install R-${R_VERSION}-1-1.x86_64.rpm
133
134scl enable devtoolset-7 /opt/R/4.0.0/bin/R
135install.packages("devtools")
136devtools::install_github('rstudio/reticulate')
137reticulate::use_condaenv("tf", conda = "~/anaconda3/bin/conda")
138reticulate::repl_python()
139# 'import tensorflow' resulted in "core dumped"
140

I guess the issue is with R/CentOS, as you can import and use tensorflow via python normally, but I'm not sure what else to try.

I would also like to say that I had no issues with Ubuntu (which is specifically supported by tensorflow, along with macOS and Windows), and I came across these docs that might be some help: https://wiki.hpcc.msu.edu/display/ITH/Installing+TensorFlow+using+anaconda / https://wiki.hpcc.msu.edu/pages/viewpage.action?pageId=22709999

Source https://stackoverflow.com/questions/70645074

QUESTION

Saving model on Tensorflow 2.7.0 with data augmentation layer

Asked 2022-Feb-04 at 17:25

I am getting an error when trying to save a model with data augmentation layers with Tensorflow version 2.7.0.

Here is the code of data augmentation:

1input_shape_rgb = (img_height, img_width, 3)
2data_augmentation_rgb = tf.keras.Sequential(
3  [ 
4    layers.RandomFlip("horizontal"),
5    layers.RandomFlip("vertical"),
6    layers.RandomRotation(0.5),
7    layers.RandomZoom(0.5),
8    layers.RandomContrast(0.5),
9    RandomColorDistortion(name='random_contrast_brightness/none'),
10  ]
11)
12

Now I build my model like this:

1input_shape_rgb = (img_height, img_width, 3)
2data_augmentation_rgb = tf.keras.Sequential(
3  [ 
4    layers.RandomFlip("horizontal"),
5    layers.RandomFlip("vertical"),
6    layers.RandomRotation(0.5),
7    layers.RandomZoom(0.5),
8    layers.RandomContrast(0.5),
9    RandomColorDistortion(name='random_contrast_brightness/none'),
10  ]
11)
12# Build the model
13input_shape = (img_height, img_width, 3)
14
15model = Sequential([
16  layers.Input(input_shape),
17  data_augmentation_rgb,
18  layers.Rescaling((1./255)),
19
20  layers.Conv2D(16, kernel_size, padding=padding, activation='relu', strides=1, 
21     data_format='channels_last'),
22  layers.MaxPooling2D(),
23  layers.BatchNormalization(),
24
25  layers.Conv2D(32, kernel_size, padding=padding, activation='relu'), # best 4
26  layers.MaxPooling2D(),
27  layers.BatchNormalization(),
28
29  layers.Conv2D(64, kernel_size, padding=padding, activation='relu'), # best 3
30  layers.MaxPooling2D(),
31  layers.BatchNormalization(),
32
33  layers.Conv2D(128, kernel_size, padding=padding, activation='relu'), # best 3
34  layers.MaxPooling2D(),
35  layers.BatchNormalization(),
36
37  layers.Flatten(),
38  layers.Dense(128, activation='relu'), # best 1
39  layers.Dropout(0.1),
40  layers.Dense(128, activation='relu'), # best 1
41  layers.Dropout(0.1),
42  layers.Dense(64, activation='relu'), # best 1
43  layers.Dropout(0.1),
44  layers.Dense(num_classes, activation = 'softmax')
45 ])
46
47 model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=metrics)
48 model.summary()
49

Then after the training is done I just make:

1input_shape_rgb = (img_height, img_width, 3)
2data_augmentation_rgb = tf.keras.Sequential(
3  [ 
4    layers.RandomFlip("horizontal"),
5    layers.RandomFlip("vertical"),
6    layers.RandomRotation(0.5),
7    layers.RandomZoom(0.5),
8    layers.RandomContrast(0.5),
9    RandomColorDistortion(name='random_contrast_brightness/none'),
10  ]
11)
12# Build the model
13input_shape = (img_height, img_width, 3)
14
15model = Sequential([
16  layers.Input(input_shape),
17  data_augmentation_rgb,
18  layers.Rescaling((1./255)),
19
20  layers.Conv2D(16, kernel_size, padding=padding, activation='relu', strides=1, 
21     data_format='channels_last'),
22  layers.MaxPooling2D(),
23  layers.BatchNormalization(),
24
25  layers.Conv2D(32, kernel_size, padding=padding, activation='relu'), # best 4
26  layers.MaxPooling2D(),
27  layers.BatchNormalization(),
28
29  layers.Conv2D(64, kernel_size, padding=padding, activation='relu'), # best 3
30  layers.MaxPooling2D(),
31  layers.BatchNormalization(),
32
33  layers.Conv2D(128, kernel_size, padding=padding, activation='relu'), # best 3
34  layers.MaxPooling2D(),
35  layers.BatchNormalization(),
36
37  layers.Flatten(),
38  layers.Dense(128, activation='relu'), # best 1
39  layers.Dropout(0.1),
40  layers.Dense(128, activation='relu'), # best 1
41  layers.Dropout(0.1),
42  layers.Dense(64, activation='relu'), # best 1
43  layers.Dropout(0.1),
44  layers.Dense(num_classes, activation = 'softmax')
45 ])
46
47 model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=metrics)
48 model.summary()
49model.save("./")
50

And I'm getting this error:

1input_shape_rgb = (img_height, img_width, 3)
2data_augmentation_rgb = tf.keras.Sequential(
3  [ 
4    layers.RandomFlip("horizontal"),
5    layers.RandomFlip("vertical"),
6    layers.RandomRotation(0.5),
7    layers.RandomZoom(0.5),
8    layers.RandomContrast(0.5),
9    RandomColorDistortion(name='random_contrast_brightness/none'),
10  ]
11)
12# Build the model
13input_shape = (img_height, img_width, 3)
14
15model = Sequential([
16  layers.Input(input_shape),
17  data_augmentation_rgb,
18  layers.Rescaling((1./255)),
19
20  layers.Conv2D(16, kernel_size, padding=padding, activation='relu', strides=1, 
21     data_format='channels_last'),
22  layers.MaxPooling2D(),
23  layers.BatchNormalization(),
24
25  layers.Conv2D(32, kernel_size, padding=padding, activation='relu'), # best 4
26  layers.MaxPooling2D(),
27  layers.BatchNormalization(),
28
29  layers.Conv2D(64, kernel_size, padding=padding, activation='relu'), # best 3
30  layers.MaxPooling2D(),
31  layers.BatchNormalization(),
32
33  layers.Conv2D(128, kernel_size, padding=padding, activation='relu'), # best 3
34  layers.MaxPooling2D(),
35  layers.BatchNormalization(),
36
37  layers.Flatten(),
38  layers.Dense(128, activation='relu'), # best 1
39  layers.Dropout(0.1),
40  layers.Dense(128, activation='relu'), # best 1
41  layers.Dropout(0.1),
42  layers.Dense(64, activation='relu'), # best 1
43  layers.Dropout(0.1),
44  layers.Dense(num_classes, activation = 'softmax')
45 ])
46
47 model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=metrics)
48 model.summary()
49model.save("./")
50---------------------------------------------------------------------------
51KeyError                                  Traceback (most recent call last)
52<ipython-input-84-87d3f09f8bee> in <module>()
53----> 1 model.save("./")
54
55
56/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in 
57 error_handler(*args, **kwargs)
58 65     except Exception as e:  # pylint: disable=broad-except
59 66       filtered_tb = _process_traceback_frames(e.__traceback__)
60 ---> 67       raise e.with_traceback(filtered_tb) from None
61 68     finally:
62 69       del filtered_tb
63
64 /usr/local/lib/python3.7/dist- 
65 packages/tensorflow/python/saved_model/function_serialization.py in 
66 serialize_concrete_function(concrete_function, node_ids, coder)
67 66   except KeyError:
68 67     raise KeyError(
69 ---> 68         f"Failed to add concrete function '{concrete_function.name}' to 
70 object-"
71 69         f"based SavedModel as it captures tensor {capture!r} which is 
72 unsupported"
73 70         " or not reachable from root. "
74
75 KeyError: "Failed to add concrete function 
76 'b'__inference_sequential_46_layer_call_fn_662953'' to object-based SavedModel as it 
77 captures tensor <tf.Tensor: shape=(), dtype=resource, value=<Resource Tensor>> which 
78 is unsupported or not reachable from root. One reason could be that a stateful 
79 object or a variable that the function depends on is not assigned to an attribute of 
80 the serialized trackable object (see SaveTest.test_captures_unreachable_variable)."
81

I inspected the reason of getting this error by changing the architecture of my model and I just found that reason came from the data_augmentation layer since the RandomFlip and RandomRotation and others are changed from layers.experimental.prepocessing.RandomFlip to layers.RandomFlip, but still the error appears.

ANSWER

Answered 2022-Feb-04 at 17:25

This seems to be a bug in Tensorflow 2.7 when using model.save combined with the parameter save_format="tf", which is set by default. The layers RandomFlip, RandomRotation, RandomZoom, and RandomContrast are causing the problems, since they are not serializable. Interestingly, the Rescaling layer can be saved without any problems. A workaround would be to simply save your model with the older Keras H5 format model.save("test", save_format='h5'):

1input_shape_rgb = (img_height, img_width, 3)
2data_augmentation_rgb = tf.keras.Sequential(
3  [ 
4    layers.RandomFlip("horizontal"),
5    layers.RandomFlip("vertical"),
6    layers.RandomRotation(0.5),
7    layers.RandomZoom(0.5),
8    layers.RandomContrast(0.5),
9    RandomColorDistortion(name='random_contrast_brightness/none'),
10  ]
11)
12# Build the model
13input_shape = (img_height, img_width, 3)
14
15model = Sequential([
16  layers.Input(input_shape),
17  data_augmentation_rgb,
18  layers.Rescaling((1./255)),
19
20  layers.Conv2D(16, kernel_size, padding=padding, activation='relu', strides=1, 
21     data_format='channels_last'),
22  layers.MaxPooling2D(),
23  layers.BatchNormalization(),
24
25  layers.Conv2D(32, kernel_size, padding=padding, activation='relu'), # best 4
26  layers.MaxPooling2D(),
27  layers.BatchNormalization(),
28
29  layers.Conv2D(64, kernel_size, padding=padding, activation='relu'), # best 3
30  layers.MaxPooling2D(),
31  layers.BatchNormalization(),
32
33  layers.Conv2D(128, kernel_size, padding=padding, activation='relu'), # best 3
34  layers.MaxPooling2D(),
35  layers.BatchNormalization(),
36
37  layers.Flatten(),
38  layers.Dense(128, activation='relu'), # best 1
39  layers.Dropout(0.1),
40  layers.Dense(128, activation='relu'), # best 1
41  layers.Dropout(0.1),
42  layers.Dense(64, activation='relu'), # best 1
43  layers.Dropout(0.1),
44  layers.Dense(num_classes, activation = 'softmax')
45 ])
46
47 model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=metrics)
48 model.summary()
49model.save("./")
50---------------------------------------------------------------------------
51KeyError                                  Traceback (most recent call last)
52<ipython-input-84-87d3f09f8bee> in <module>()
53----> 1 model.save("./")
54
55
56/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in 
57 error_handler(*args, **kwargs)
58 65     except Exception as e:  # pylint: disable=broad-except
59 66       filtered_tb = _process_traceback_frames(e.__traceback__)
60 ---> 67       raise e.with_traceback(filtered_tb) from None
61 68     finally:
62 69       del filtered_tb
63
64 /usr/local/lib/python3.7/dist- 
65 packages/tensorflow/python/saved_model/function_serialization.py in 
66 serialize_concrete_function(concrete_function, node_ids, coder)
67 66   except KeyError:
68 67     raise KeyError(
69 ---> 68         f"Failed to add concrete function '{concrete_function.name}' to 
70 object-"
71 69         f"based SavedModel as it captures tensor {capture!r} which is 
72 unsupported"
73 70         " or not reachable from root. "
74
75 KeyError: "Failed to add concrete function 
76 'b'__inference_sequential_46_layer_call_fn_662953'' to object-based SavedModel as it 
77 captures tensor <tf.Tensor: shape=(), dtype=resource, value=<Resource Tensor>> which 
78 is unsupported or not reachable from root. One reason could be that a stateful 
79 object or a variable that the function depends on is not assigned to an attribute of 
80 the serialized trackable object (see SaveTest.test_captures_unreachable_variable)."
81import tensorflow as tf
82import numpy as np
83
84class RandomColorDistortion(tf.keras.layers.Layer):
85    def __init__(self, contrast_range=[0.5, 1.5], 
86                 brightness_delta=[-0.2, 0.2], **kwargs):
87        super(RandomColorDistortion, self).__init__(**kwargs)
88        self.contrast_range = contrast_range
89        self.brightness_delta = brightness_delta
90    
91    def call(self, images, training=None):
92        if not training:
93            return images
94        contrast = np.random.uniform(
95            self.contrast_range[0], self.contrast_range[1])
96        brightness = np.random.uniform(
97            self.brightness_delta[0], self.brightness_delta[1])
98        
99        images = tf.image.adjust_contrast(images, contrast)
100        images = tf.image.adjust_brightness(images, brightness)
101        images = tf.clip_by_value(images, 0, 1)
102        return images
103    
104    def get_config(self):
105        config = super(RandomColorDistortion, self).get_config()
106        config.update({"contrast_range": self.contrast_range, "brightness_delta": self.brightness_delta})
107        return config
108        
109input_shape_rgb = (256, 256, 3)
110data_augmentation_rgb = tf.keras.Sequential(
111  [ 
112    tf.keras.layers.RandomFlip("horizontal"),
113    tf.keras.layers.RandomFlip("vertical"),
114    tf.keras.layers.RandomRotation(0.5),
115    tf.keras.layers.RandomZoom(0.5),
116    tf.keras.layers.RandomContrast(0.5),
117    RandomColorDistortion(name='random_contrast_brightness/none'),
118  ]
119)
120input_shape = (256, 256, 3)
121padding = 'same'
122kernel_size = 3
123model = tf.keras.Sequential([
124  tf.keras.layers.Input(input_shape),
125  data_augmentation_rgb,
126  tf.keras.layers.Rescaling((1./255)),
127  tf.keras.layers.Conv2D(16, kernel_size, padding=padding, activation='relu', strides=1, 
128     data_format='channels_last'),
129  tf.keras.layers.MaxPooling2D(),
130  tf.keras.layers.BatchNormalization(),
131
132  tf.keras.layers.Conv2D(32, kernel_size, padding=padding, activation='relu'), # best 4
133  tf.keras.layers.MaxPooling2D(),
134  tf.keras.layers.BatchNormalization(),
135
136  tf.keras.layers.Conv2D(64, kernel_size, padding=padding, activation='relu'), # best 3
137  tf.keras.layers.MaxPooling2D(),
138  tf.keras.layers.BatchNormalization(),
139
140  tf.keras.layers.Conv2D(128, kernel_size, padding=padding, activation='relu'), # best 3
141  tf.keras.layers.MaxPooling2D(),
142  tf.keras.layers.BatchNormalization(),
143
144  tf.keras.layers.Flatten(),
145  tf.keras.layers.Dense(128, activation='relu'), # best 1
146  tf.keras.layers.Dropout(0.1),
147  tf.keras.layers.Dense(128, activation='relu'), # best 1
148  tf.keras.layers.Dropout(0.1),
149  tf.keras.layers.Dense(64, activation='relu'), # best 1
150  tf.keras.layers.Dropout(0.1),
151  tf.keras.layers.Dense(5, activation = 'softmax')
152 ])
153
154model.compile(loss='categorical_crossentropy', optimizer='adam')
155model.summary()
156model.save("test", save_format='h5')
157

Loading your model with your custom layer would look like this then:

1input_shape_rgb = (img_height, img_width, 3)
2data_augmentation_rgb = tf.keras.Sequential(
3  [ 
4    layers.RandomFlip("horizontal"),
5    layers.RandomFlip("vertical"),
6    layers.RandomRotation(0.5),
7    layers.RandomZoom(0.5),
8    layers.RandomContrast(0.5),
9    RandomColorDistortion(name='random_contrast_brightness/none'),
10  ]
11)
12# Build the model
13input_shape = (img_height, img_width, 3)
14
15model = Sequential([
16  layers.Input(input_shape),
17  data_augmentation_rgb,
18  layers.Rescaling((1./255)),
19
20  layers.Conv2D(16, kernel_size, padding=padding, activation='relu', strides=1, 
21     data_format='channels_last'),
22  layers.MaxPooling2D(),
23  layers.BatchNormalization(),
24
25  layers.Conv2D(32, kernel_size, padding=padding, activation='relu'), # best 4
26  layers.MaxPooling2D(),
27  layers.BatchNormalization(),
28
29  layers.Conv2D(64, kernel_size, padding=padding, activation='relu'), # best 3
30  layers.MaxPooling2D(),
31  layers.BatchNormalization(),
32
33  layers.Conv2D(128, kernel_size, padding=padding, activation='relu'), # best 3
34  layers.MaxPooling2D(),
35  layers.BatchNormalization(),
36
37  layers.Flatten(),
38  layers.Dense(128, activation='relu'), # best 1
39  layers.Dropout(0.1),
40  layers.Dense(128, activation='relu'), # best 1
41  layers.Dropout(0.1),
42  layers.Dense(64, activation='relu'), # best 1
43  layers.Dropout(0.1),
44  layers.Dense(num_classes, activation = 'softmax')
45 ])
46
47 model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=metrics)
48 model.summary()
49model.save("./")
50---------------------------------------------------------------------------
51KeyError                                  Traceback (most recent call last)
52<ipython-input-84-87d3f09f8bee> in <module>()
53----> 1 model.save("./")
54
55
56/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in 
57 error_handler(*args, **kwargs)
58 65     except Exception as e:  # pylint: disable=broad-except
59 66       filtered_tb = _process_traceback_frames(e.__traceback__)
60 ---> 67       raise e.with_traceback(filtered_tb) from None
61 68     finally:
62 69       del filtered_tb
63
64 /usr/local/lib/python3.7/dist- 
65 packages/tensorflow/python/saved_model/function_serialization.py in 
66 serialize_concrete_function(concrete_function, node_ids, coder)
67 66   except KeyError:
68 67     raise KeyError(
69 ---> 68         f"Failed to add concrete function '{concrete_function.name}' to 
70 object-"
71 69         f"based SavedModel as it captures tensor {capture!r} which is 
72 unsupported"
73 70         " or not reachable from root. "
74
75 KeyError: "Failed to add concrete function 
76 'b'__inference_sequential_46_layer_call_fn_662953'' to object-based SavedModel as it 
77 captures tensor <tf.Tensor: shape=(), dtype=resource, value=<Resource Tensor>> which 
78 is unsupported or not reachable from root. One reason could be that a stateful 
79 object or a variable that the function depends on is not assigned to an attribute of 
80 the serialized trackable object (see SaveTest.test_captures_unreachable_variable)."
81import tensorflow as tf
82import numpy as np
83
84class RandomColorDistortion(tf.keras.layers.Layer):
85    def __init__(self, contrast_range=[0.5, 1.5], 
86                 brightness_delta=[-0.2, 0.2], **kwargs):
87        super(RandomColorDistortion, self).__init__(**kwargs)
88        self.contrast_range = contrast_range
89        self.brightness_delta = brightness_delta
90    
91    def call(self, images, training=None):
92        if not training:
93            return images
94        contrast = np.random.uniform(
95            self.contrast_range[0], self.contrast_range[1])
96        brightness = np.random.uniform(
97            self.brightness_delta[0], self.brightness_delta[1])
98        
99        images = tf.image.adjust_contrast(images, contrast)
100        images = tf.image.adjust_brightness(images, brightness)
101        images = tf.clip_by_value(images, 0, 1)
102        return images
103    
104    def get_config(self):
105        config = super(RandomColorDistortion, self).get_config()
106        config.update({"contrast_range": self.contrast_range, "brightness_delta": self.brightness_delta})
107        return config
108        
109input_shape_rgb = (256, 256, 3)
110data_augmentation_rgb = tf.keras.Sequential(
111  [ 
112    tf.keras.layers.RandomFlip("horizontal"),
113    tf.keras.layers.RandomFlip("vertical"),
114    tf.keras.layers.RandomRotation(0.5),
115    tf.keras.layers.RandomZoom(0.5),
116    tf.keras.layers.RandomContrast(0.5),
117    RandomColorDistortion(name='random_contrast_brightness/none'),
118  ]
119)
120input_shape = (256, 256, 3)
121padding = 'same'
122kernel_size = 3
123model = tf.keras.Sequential([
124  tf.keras.layers.Input(input_shape),
125  data_augmentation_rgb,
126  tf.keras.layers.Rescaling((1./255)),
127  tf.keras.layers.Conv2D(16, kernel_size, padding=padding, activation='relu', strides=1, 
128     data_format='channels_last'),
129  tf.keras.layers.MaxPooling2D(),
130  tf.keras.layers.BatchNormalization(),
131
132  tf.keras.layers.Conv2D(32, kernel_size, padding=padding, activation='relu'), # best 4
133  tf.keras.layers.MaxPooling2D(),
134  tf.keras.layers.BatchNormalization(),
135
136  tf.keras.layers.Conv2D(64, kernel_size, padding=padding, activation='relu'), # best 3
137  tf.keras.layers.MaxPooling2D(),
138  tf.keras.layers.BatchNormalization(),
139
140  tf.keras.layers.Conv2D(128, kernel_size, padding=padding, activation='relu'), # best 3
141  tf.keras.layers.MaxPooling2D(),
142  tf.keras.layers.BatchNormalization(),
143
144  tf.keras.layers.Flatten(),
145  tf.keras.layers.Dense(128, activation='relu'), # best 1
146  tf.keras.layers.Dropout(0.1),
147  tf.keras.layers.Dense(128, activation='relu'), # best 1
148  tf.keras.layers.Dropout(0.1),
149  tf.keras.layers.Dense(64, activation='relu'), # best 1
150  tf.keras.layers.Dropout(0.1),
151  tf.keras.layers.Dense(5, activation = 'softmax')
152 ])
153
154model.compile(loss='categorical_crossentropy', optimizer='adam')
155model.summary()
156model.save("test", save_format='h5')
157model = tf.keras.models.load_model('test.h5', custom_objects={'RandomColorDistortion': RandomColorDistortion})
158

where RandomColorDistortion is the name of your custom layer.

Source https://stackoverflow.com/questions/69955838

QUESTION

OpenVino converted model not returning same score values as original model (Sigmoid)

Asked 2022-Jan-05 at 06:06

I've converted a Keras model for use with OpenVino. The original Keras model used sigmoid to return scores ranging from 0 to 1 for binary classification. After converting the model for use with OpenVino, the scores are all near 0.99 for both classes but seem slightly lower for one of the classes.

For example, test1.jpg and test2.jpg (from opposite classes) yield scores of 0.00320357 and 0.9999, respectively.

With OpenVino, the same images yield scores of 0.9998982 and 0.9962392, respectively.

Edit* One suspicion is that the input array is still accepted by the OpenVino model but is somehow changed in shape or "scrambled" and therefore is never a match for class one? In other words, if you fed it random noise, the score would also always be 0.9999. Maybe I'd have to somehow get the OpenVino model to accept the original shape (1,180,180,3) instead of (1,3,180,180) so I don't have to force the input into a different shape than the one the original model accepted? That's weird though because I specified the shape when making the xml and bin for openvino:

1python3 /opt/intel/openvino_2021/deployment_tools/model_optimizer/mo_tf.py --saved_model_dir /Users/.../Desktop/.../model13 --output_dir /Users/.../Desktop/... --input_shape=\[1,180,180,3]
2

However, I know from error messages that the inference engine is expecting (1,3,180,180) for some unknown reason. Could that be the problem? The other suspicion is something wrong with how the original model was frozen. I'm exploring different ways to freeze the original model (keras model converted to pb) in case the problem is related to that.

I checked to make sure the Sigmoid activation function is being used in the OpenVino implementation (same activation as the Keras model) and it looks like it is. Why, then, are the values not the same? Any help would be much appreciated.

The code for the OpenVino inference is:

1python3 /opt/intel/openvino_2021/deployment_tools/model_optimizer/mo_tf.py --saved_model_dir /Users/.../Desktop/.../model13 --output_dir /Users/.../Desktop/... --input_shape=\[1,180,180,3]
2import openvino
3from openvino.inference_engine import IECore, IENetwork 
4from skimage import io
5import sys
6import numpy as np
7import os
8
9def loadNetwork(model_xml, model_bin):
10
11    ie = IECore() 
12
13    network =  ie.read_network(model=model_xml, weights=model_bin)
14
15    input_placeholder_key = list(network.input_info)[0]
16    input_placeholder = network.input_info[input_placeholder_key]
17
18    output_placeholder_key = list(network.outputs)[0]
19    output_placeholder = network.outputs[output_placeholder_key]
20
21    return network, input_placeholder_key, output_placeholder_key
22
23batch_size = 1
24channels = 3
25IMG_HEIGHT = 180
26IMG_WIDTH = 180
27
28#loadNetwork('saved_model.xml','saved_model.bin')
29
30image_path = 'test.jpg'
31
32def load_source(path_to_image):
33    image = io.imread(path_to_image)
34    img = np.resize(image,(180,180))
35    return img
36
37img_new = load_source('test2.jpg')
38
39#Batch?
40
41def classify(image):
42    device = 'CPU'
43    network, input_placeholder_key, output_placeholder_key = loadNetwork('saved_model.xml','saved_model.bin')
44    ie = IECore() 
45    exec_net = ie.load_network(network=network, device_name=device)
46    res = exec_net.infer(inputs={input_placeholder_key: image})
47    print(res)
48    res = res[output_placeholder_key]
49    return res
50
51result = classify(img_new)
52print(result)
53result = result[0]
54top_result = np.argmax(result)
55print(top_result)
56print(result[top_result])
57

And the result:

1python3 /opt/intel/openvino_2021/deployment_tools/model_optimizer/mo_tf.py --saved_model_dir /Users/.../Desktop/.../model13 --output_dir /Users/.../Desktop/... --input_shape=\[1,180,180,3]
2import openvino
3from openvino.inference_engine import IECore, IENetwork 
4from skimage import io
5import sys
6import numpy as np
7import os
8
9def loadNetwork(model_xml, model_bin):
10
11    ie = IECore() 
12
13    network =  ie.read_network(model=model_xml, weights=model_bin)
14
15    input_placeholder_key = list(network.input_info)[0]
16    input_placeholder = network.input_info[input_placeholder_key]
17
18    output_placeholder_key = list(network.outputs)[0]
19    output_placeholder = network.outputs[output_placeholder_key]
20
21    return network, input_placeholder_key, output_placeholder_key
22
23batch_size = 1
24channels = 3
25IMG_HEIGHT = 180
26IMG_WIDTH = 180
27
28#loadNetwork('saved_model.xml','saved_model.bin')
29
30image_path = 'test.jpg'
31
32def load_source(path_to_image):
33    image = io.imread(path_to_image)
34    img = np.resize(image,(180,180))
35    return img
36
37img_new = load_source('test2.jpg')
38
39#Batch?
40
41def classify(image):
42    device = 'CPU'
43    network, input_placeholder_key, output_placeholder_key = loadNetwork('saved_model.xml','saved_model.bin')
44    ie = IECore() 
45    exec_net = ie.load_network(network=network, device_name=device)
46    res = exec_net.infer(inputs={input_placeholder_key: image})
47    print(res)
48    res = res[output_placeholder_key]
49    return res
50
51result = classify(img_new)
52print(result)
53result = result[0]
54top_result = np.argmax(result)
55print(top_result)
56print(result[top_result])
57{'StatefulPartitionedCall/model/dense/Sigmoid': array([[0.9962392]], dtype=float32)}
58[[0.9962392]]
590
600.9962392
61

ANSWER

Answered 2022-Jan-05 at 06:06

Generally, Tensorflow is the only network with the shape NHWC while most others use NCHW. Thus, the OpenVINO Inference Engine satisfies the majority of networks and uses the NCHW layout. Model must be converted to NCHW layout in order to work with Inference Engine.

The conversion of the native model format into IR involves the process where the Model Optimizer performs the necessary transformation to convert the shape to the layout required by the Inference Engine (N,C,H,W). Using the --input_shape parameter with the correct input shape of the model should suffice.

Besides, most TensorFlow models are trained with images in RGB order. In this case, inference results using the Inference Engine samples may be incorrect. By default, Inference Engine samples and demos expect input with BGR channels order. If you trained your model to work with RGB order, you need to manually rearrange the default channels order in the sample or demo application or reconvert your model using the Model Optimizer tool with --reverse_input_channels argument.

I suggest you validate this by inferring your model with the Hello Classification Python Sample instead since this is one of the official samples provided to test the model's functionality.

You may refer to this "Intel Math Kernel Library for Deep Neural Network" for deeper explanation regarding the input shape.

Source https://stackoverflow.com/questions/70546922

QUESTION

Is it possible to use a collection of hyperspectral 1x1 pixels in a CNN model purposed for more conventional datasets (CIFAR-10/MNIST)?

Asked 2021-Dec-17 at 09:08

I have created a working CNN model in Keras/Tensorflow, and have successfully used the CIFAR-10 & MNIST datasets to test this model. The functioning code as seen below:

1import keras
2from keras.datasets import cifar10
3from keras.utils import to_categorical
4from keras.models import Sequential
5from keras.layers import Dense, Activation, Dropout, Conv2D, Flatten, MaxPooling2D
6from keras.layers.normalization import BatchNormalization
7
8(X_train, y_train), (X_test, y_test) = cifar10.load_data()
9
10#reshape data to fit model
11X_train = X_train.reshape(50000,32,32,3)
12X_test = X_test.reshape(10000,32,32,3)
13
14y_train = to_categorical(y_train)
15y_test = to_categorical(y_test)
16
17
18# Building the model 
19
20#1st Convolutional Layer
21model.add(Conv2D(filters=64, input_shape=(32,32,3), kernel_size=(11,11), strides=(4,4), padding='same'))
22model.add(BatchNormalization())
23model.add(Activation('relu'))
24model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'))
25
26#2nd Convolutional Layer
27model.add(Conv2D(filters=224, kernel_size=(5, 5), strides=(1,1), padding='same'))
28model.add(BatchNormalization())
29model.add(Activation('relu'))
30model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'))
31
32#3rd Convolutional Layer
33model.add(Conv2D(filters=288, kernel_size=(3,3), strides=(1,1), padding='same'))
34model.add(BatchNormalization())
35model.add(Activation('relu'))
36
37#4th Convolutional Layer
38model.add(Conv2D(filters=288, kernel_size=(3,3), strides=(1,1), padding='same'))
39model.add(BatchNormalization())
40model.add(Activation('relu'))
41
42#5th Convolutional Layer
43model.add(Conv2D(filters=160, kernel_size=(3,3), strides=(1,1), padding='same'))
44model.add(BatchNormalization())
45model.add(Activation('relu'))
46model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'))
47
48model.add(Flatten())
49
50# 1st Fully Connected Layer
51model.add(Dense(4096, input_shape=(32,32,3,)))
52model.add(BatchNormalization())
53model.add(Activation('relu'))
54# Add Dropout to prevent overfitting
55model.add(Dropout(0.4))
56
57#2nd Fully Connected Layer
58model.add(Dense(4096))
59model.add(BatchNormalization())
60model.add(Activation('relu'))
61#Add Dropout
62model.add(Dropout(0.4))
63
64#3rd Fully Connected Layer
65model.add(Dense(1000))
66model.add(BatchNormalization())
67model.add(Activation('relu'))
68#Add Dropout
69model.add(Dropout(0.4))
70
71#Output Layer
72model.add(Dense(10))
73model.add(BatchNormalization())
74model.add(Activation('softmax'))
75
76
77#compile model using accuracy to measure model performance
78opt = keras.optimizers.Adam(learning_rate = 0.0001)
79model.compile(optimizer=opt, loss='categorical_crossentropy', 
80              metrics=['accuracy'])
81
82
83#train the model
84model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=30)
85

From this point after utilising the aforementioned datasets, I wanted to go one further and use a dataset with more channels than a greyscale or rgb presented, hence the inclusion of a hyperspectral dataset. When looking for a hyperspectral dataset I came across this one.

The issue at this stage was realising that this hyperspectral dataset was one image, with each value in the ground truth relating to each pixel. At this stage I reformatted the data from this into a collection of hyperspectral data/pixels.

Code reformatting corrected dataset for x_train & x_test:

1import keras
2from keras.datasets import cifar10
3from keras.utils import to_categorical
4from keras.models import Sequential
5from keras.layers import Dense, Activation, Dropout, Conv2D, Flatten, MaxPooling2D
6from keras.layers.normalization import BatchNormalization
7
8(X_train, y_train), (X_test, y_test) = cifar10.load_data()
9
10#reshape data to fit model
11X_train = X_train.reshape(50000,32,32,3)
12X_test = X_test.reshape(10000,32,32,3)
13
14y_train = to_categorical(y_train)
15y_test = to_categorical(y_test)
16
17
18# Building the model 
19
20#1st Convolutional Layer
21model.add(Conv2D(filters=64, input_shape=(32,32,3), kernel_size=(11,11), strides=(4,4), padding='same'))
22model.add(BatchNormalization())
23model.add(Activation('relu'))
24model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'))
25
26#2nd Convolutional Layer
27model.add(Conv2D(filters=224, kernel_size=(5, 5), strides=(1,1), padding='same'))
28model.add(BatchNormalization())
29model.add(Activation('relu'))
30model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'))
31
32#3rd Convolutional Layer
33model.add(Conv2D(filters=288, kernel_size=(3,3), strides=(1,1), padding='same'))
34model.add(BatchNormalization())
35model.add(Activation('relu'))
36
37#4th Convolutional Layer
38model.add(Conv2D(filters=288, kernel_size=(3,3), strides=(1,1), padding='same'))
39model.add(BatchNormalization())
40model.add(Activation('relu'))
41
42#5th Convolutional Layer
43model.add(Conv2D(filters=160, kernel_size=(3,3), strides=(1,1), padding='same'))
44model.add(BatchNormalization())
45model.add(Activation('relu'))
46model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'))
47
48model.add(Flatten())
49
50# 1st Fully Connected Layer
51model.add(Dense(4096, input_shape=(32,32,3,)))
52model.add(BatchNormalization())
53model.add(Activation('relu'))
54# Add Dropout to prevent overfitting
55model.add(Dropout(0.4))
56
57#2nd Fully Connected Layer
58model.add(Dense(4096))
59model.add(BatchNormalization())
60model.add(Activation('relu'))
61#Add Dropout
62model.add(Dropout(0.4))
63
64#3rd Fully Connected Layer
65model.add(Dense(1000))
66model.add(BatchNormalization())
67model.add(Activation('relu'))
68#Add Dropout
69model.add(Dropout(0.4))
70
71#Output Layer
72model.add(Dense(10))
73model.add(BatchNormalization())
74model.add(Activation('softmax'))
75
76
77#compile model using accuracy to measure model performance
78opt = keras.optimizers.Adam(learning_rate = 0.0001)
79model.compile(optimizer=opt, loss='categorical_crossentropy', 
80              metrics=['accuracy'])
81
82
83#train the model
84model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=30)
85import keras
86import scipy
87import numpy as np
88import matplotlib.pyplot as plt
89from keras.utils import to_categorical
90from scipy import io
91
92mydict = scipy.io.loadmat('Indian_pines_corrected.mat')
93dataset = np.array(mydict.get('indian_pines_corrected'))
94
95
96#This is creating the split between x_train and x_test from the original dataset 
97# x_train after this code runs will have a shape of (121, 145, 200) 
98# x_test after this code runs will have a shape of (24, 145, 200)
99x_train = np.zeros((121,145,200), dtype=np.int)
100x_test = np.zeros((24,145,200), dtype=np.int)    
101
102xtemp = np.array_split(dataset, [121])
103x_train = np.array(xtemp[0])
104x_test = np.array(xtemp[1])
105
106# x_train will have a shape of (17545, 200) 
107# x_test will have a shape of (3480, 200)
108x_train = x_train.reshape(-1, x_train.shape[-1])
109x_test = x_test.reshape(-1, x_test.shape[-1])
110

Code reformatting ground truth dataset for Y_train & Y_test:

1import keras
2from keras.datasets import cifar10
3from keras.utils import to_categorical
4from keras.models import Sequential
5from keras.layers import Dense, Activation, Dropout, Conv2D, Flatten, MaxPooling2D
6from keras.layers.normalization import BatchNormalization
7
8(X_train, y_train), (X_test, y_test) = cifar10.load_data()
9
10#reshape data to fit model
11X_train = X_train.reshape(50000,32,32,3)
12X_test = X_test.reshape(10000,32,32,3)
13
14y_train = to_categorical(y_train)
15y_test = to_categorical(y_test)
16
17
18# Building the model 
19
20#1st Convolutional Layer
21model.add(Conv2D(filters=64, input_shape=(32,32,3), kernel_size=(11,11), strides=(4,4), padding='same'))
22model.add(BatchNormalization())
23model.add(Activation('relu'))
24model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'))
25
26#2nd Convolutional Layer
27model.add(Conv2D(filters=224, kernel_size=(5, 5), strides=(1,1), padding='same'))
28model.add(BatchNormalization())
29model.add(Activation('relu'))
30model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'))
31
32#3rd Convolutional Layer
33model.add(Conv2D(filters=288, kernel_size=(3,3), strides=(1,1), padding='same'))
34model.add(BatchNormalization())
35model.add(Activation('relu'))
36
37#4th Convolutional Layer
38model.add(Conv2D(filters=288, kernel_size=(3,3), strides=(1,1), padding='same'))
39model.add(BatchNormalization())
40model.add(Activation('relu'))
41
42#5th Convolutional Layer
43model.add(Conv2D(filters=160, kernel_size=(3,3), strides=(1,1), padding='same'))
44model.add(BatchNormalization())
45model.add(Activation('relu'))
46model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'))
47
48model.add(Flatten())
49
50# 1st Fully Connected Layer
51model.add(Dense(4096, input_shape=(32,32,3,)))
52model.add(BatchNormalization())
53model.add(Activation('relu'))
54# Add Dropout to prevent overfitting
55model.add(Dropout(0.4))
56
57#2nd Fully Connected Layer
58model.add(Dense(4096))
59model.add(BatchNormalization())
60model.add(Activation('relu'))
61#Add Dropout
62model.add(Dropout(0.4))
63
64#3rd Fully Connected Layer
65model.add(Dense(1000))
66model.add(BatchNormalization())
67model.add(Activation('relu'))
68#Add Dropout
69model.add(Dropout(0.4))
70
71#Output Layer
72model.add(Dense(10))
73model.add(BatchNormalization())
74model.add(Activation('softmax'))
75
76
77#compile model using accuracy to measure model performance
78opt = keras.optimizers.Adam(learning_rate = 0.0001)
79model.compile(optimizer=opt, loss='categorical_crossentropy', 
80              metrics=['accuracy'])
81
82
83#train the model
84model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=30)
85import keras
86import scipy
87import numpy as np
88import matplotlib.pyplot as plt
89from keras.utils import to_categorical
90from scipy import io
91
92mydict = scipy.io.loadmat('Indian_pines_corrected.mat')
93dataset = np.array(mydict.get('indian_pines_corrected'))
94
95
96#This is creating the split between x_train and x_test from the original dataset 
97# x_train after this code runs will have a shape of (121, 145, 200) 
98# x_test after this code runs will have a shape of (24, 145, 200)
99x_train = np.zeros((121,145,200), dtype=np.int)
100x_test = np.zeros((24,145,200), dtype=np.int)    
101
102xtemp = np.array_split(dataset, [121])
103x_train = np.array(xtemp[0])
104x_test = np.array(xtemp[1])
105
106# x_train will have a shape of (17545, 200) 
107# x_test will have a shape of (3480, 200)
108x_train = x_train.reshape(-1, x_train.shape[-1])
109x_test = x_test.reshape(-1, x_test.shape[-1])
110truthDataset = scipy.io.loadmat('Indian_pines_gt.mat')
111gTruth = truthDataset.get('indian_pines_gt')
112
113#This is creating the split between Y_train and Y_test from the original dataset 
114# Y_train after this code runs will have a shape of (121, 145) 
115# Y_test after this code runs will have a shape of (24, 145)
116
117Y_train = np.zeros((121,145), dtype=np.int)
118Y_test = np.zeros((24,145), dtype=np.int)    
119
120ytemp = np.array_split(gTruth, [121])
121Y_train = np.array(ytemp[0])
122Y_test = np.array(ytemp[1])
123
124# Y_train will have a shape of (17545) 
125# Y_test will have a shape of (3480)
126Y_train = Y_train.reshape(-1)
127Y_test = Y_test.reshape(-1)
128
129
130#17 binary categories ranging from 0-16
131
132#Y_train one-hot encode target column
133Y_train = to_categorical(Y_train)
134
135#Y_test one-hot encode target column
136Y_test = to_categorical(Y_test, num_classes = 17)
137

My thought process was that, despite the initial image being broken down into 1x1 patches, the large number of channels each patch possessed with their respective values would aid in categorisation of the dataset.

Essentially I'd want to input this reformatted data into my model (seen within the first code fragment in this post), however I'm uncertain if I am taking the wrong approach to this due to my inexperience with this area of expertise. I was expecting to input a shape of (1,1,200), i.e the shape of x_train & x_test would be (17545,1,1,200) & (3480,1,1,200) respectively.

ANSWER

Answered 2021-Dec-16 at 10:18

If the hyperspectral dataset is given to you as a large image with many channels, I suppose that the classification of each pixel should depend on the pixels around it (otherwise I would not format the data as an image, i.e. without grid structure). Given this assumption, breaking up the input picture into 1x1 parts is not a good idea as you are loosing the grid structure.

I further suppose that the order of the channels is arbitrary, which implies that convolution over the channels is probably not meaningful (which you however did not plan to do anyways).

Instead of reformatting the data the way you did, you may want to create a model that takes an image as input and also outputs an "image" containing the classifications for each pixel. I.e. if you have 10 classes and take a (145, 145, 200) image as input, your model would output a (145, 145, 10) image. In that architecture you would not have any fully-connected layers. Your output layer would also be a convolutional layer.

That however means that you will not be able to keep your current architecture. That is because the tasks for MNIST/CIFAR10 and your hyperspectral dataset are not the same. For MNIST/CIFAR10 you want to classify an image in it's entirety, while for the other dataset you want to assign a class to each pixel (while most likely also using the pixels around each pixel).


Some further ideas:

  • If you want to turn the pixel classification task on the hyperspectral dataset into a classification task for an entire image, maybe you can reformulate that task as "classifying a hyperspectral image as the class of it's center (or top-left, or bottom-right, or (21th, 104th), or whatever) pixel". To obtain the data from your single hyperspectral image, for each pixel, I would shift the image such that the target pixel is at the desired location (e.g. the center). All pixels that "fall off" the border could be inserted at the other side of the image.
  • If you want to stick with a pixel classification task but need more data, maybe split up the single hyperspectral image you have into many smaller images (e.g. 10x10x200). You may even want to use images of many different sizes. If you model only has convolution and pooling layers and you make sure to maintain the sizes of the image, that should work out.

Source https://stackoverflow.com/questions/70226626

QUESTION

ImportError: cannot import name 'BatchNormalization' from 'keras.layers.normalization'

Asked 2021-Nov-13 at 07:14

i have an import problem when executing my code:

1from keras.models import Sequential
2from keras.layers.normalization import BatchNormalization
3
1from keras.models import Sequential
2from keras.layers.normalization import BatchNormalization
32021-10-06 22:27:14.064885: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
42021-10-06 22:27:14.064974: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
5Traceback (most recent call last):
6  File "C:\Data\breast-cancer-classification\train_model.py", line 10, in <module>
7    from cancernet.cancernet import CancerNet
8  File "C:\Data\breast-cancer-classification\cancernet\cancernet.py", line 2, in <module>
9    from keras.layers.normalization import BatchNormalization
10ImportError: cannot import name 'BatchNormalization' from 'keras.layers.normalization' (C:\Users\Catalin\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\layers\normalization\__init__.py)
11
  • Keras version: 2.6.0
  • Tensorflow: 2.6.0
  • Python version: 3.9.7

The library it is installed also with

1from keras.models import Sequential
2from keras.layers.normalization import BatchNormalization
32021-10-06 22:27:14.064885: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
42021-10-06 22:27:14.064974: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
5Traceback (most recent call last):
6  File "C:\Data\breast-cancer-classification\train_model.py", line 10, in <module>
7    from cancernet.cancernet import CancerNet
8  File "C:\Data\breast-cancer-classification\cancernet\cancernet.py", line 2, in <module>
9    from keras.layers.normalization import BatchNormalization
10ImportError: cannot import name 'BatchNormalization' from 'keras.layers.normalization' (C:\Users\Catalin\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\layers\normalization\__init__.py)
11pip install numpy opencv-python pillow tensorflow keras imutils scikit-learn matplotlib
12

Do you have any ideas?

library path

ANSWER

Answered 2021-Oct-06 at 20:27

You're using outdated imports for tf.keras. Layers can now be imported directly from tensorflow.keras.layers:

1from keras.models import Sequential
2from keras.layers.normalization import BatchNormalization
32021-10-06 22:27:14.064885: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
42021-10-06 22:27:14.064974: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
5Traceback (most recent call last):
6  File "C:\Data\breast-cancer-classification\train_model.py", line 10, in <module>
7    from cancernet.cancernet import CancerNet
8  File "C:\Data\breast-cancer-classification\cancernet\cancernet.py", line 2, in <module>
9    from keras.layers.normalization import BatchNormalization
10ImportError: cannot import name 'BatchNormalization' from 'keras.layers.normalization' (C:\Users\Catalin\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\layers\normalization\__init__.py)
11pip install numpy opencv-python pillow tensorflow keras imutils scikit-learn matplotlib
12from tensorflow.keras.models import Sequential
13from tensorflow.keras.layers import (
14    BatchNormalization, SeparableConv2D, MaxPooling2D, Activation, Flatten, Dropout, Dense
15)
16from tensorflow.keras import backend as K
17
18
19class CancerNet:
20    @staticmethod
21    def build(width, height, depth, classes):
22        model = Sequential()
23        shape = (height, width, depth)
24        channelDim = -1
25
26        if K.image_data_format() == "channels_first":
27            shape = (depth, height, width)
28            channelDim = 1
29
30        model.add(SeparableConv2D(32, (3, 3), padding="same", input_shape=shape))
31        model.add(Activation("relu"))
32        model.add(BatchNormalization(axis=channelDim))
33        model.add(MaxPooling2D(pool_size=(2, 2)))
34        model.add(Dropout(0.25))
35
36        model.add(SeparableConv2D(64, (3, 3), padding="same"))
37        model.add(Activation("relu"))
38        model.add(BatchNormalization(axis=channelDim))
39        model.add(SeparableConv2D(64, (3, 3), padding="same"))
40        model.add(Activation("relu"))
41        model.add(BatchNormalization(axis=channelDim))
42        model.add(MaxPooling2D(pool_size=(2, 2)))
43        model.add(Dropout(0.25))
44
45        model.add(SeparableConv2D(128, (3, 3), padding="same"))
46        model.add(Activation("relu"))
47        model.add(BatchNormalization(axis=channelDim))
48        model.add(SeparableConv2D(128, (3, 3), padding="same"))
49        model.add(Activation("relu"))
50        model.add(BatchNormalization(axis=channelDim))
51        model.add(SeparableConv2D(128, (3, 3), padding="same"))
52        model.add(Activation("relu"))
53        model.add(BatchNormalization(axis=channelDim))
54        model.add(MaxPooling2D(pool_size=(2, 2)))
55        model.add(Dropout(0.25))
56
57        model.add(Flatten())
58        model.add(Dense(256))
59        model.add(Activation("relu"))
60        model.add(BatchNormalization())
61        model.add(Dropout(0.5))
62
63        model.add(Dense(classes))
64        model.add(Activation("softmax"))
65
66        return model
67
68model = CancerNet()
69

Source https://stackoverflow.com/questions/69471749

QUESTION

Unable to (manually) load cifar10 dataset

Asked 2021-Oct-24 at 02:47

First, I tried to load using:

1(X_train, y_train), (X_test, y_test) = datasets.cifar10.load_data()
2

But it gave an error:

1(X_train, y_train), (X_test, y_test) = datasets.cifar10.load_data()
2Exception: URL fetch failure on https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1125)
3

So I manually downloaded the dataset and put it in C:\Users\SAHAN\.keras\datasets and renamed it to cifar-10-batches-py.tar.gz.

But then it gives an error:

1(X_train, y_train), (X_test, y_test) = datasets.cifar10.load_data()
2Exception: URL fetch failure on https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1125)
3PermissionError: [Errno 13] Permission denied: 'C:\\Users\\SAHAN\\.keras\\datasets\\cifar-10-batches-py.tar.gz'
4

How can I load this dataset?

ANSWER

Answered 2021-Oct-23 at 22:57

I was having a similar CERTIFICATE_VERIFY_FAILED error downloading CIFAR-10. Putting this in my python file worked:

1(X_train, y_train), (X_test, y_test) = datasets.cifar10.load_data()
2Exception: URL fetch failure on https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1125)
3PermissionError: [Errno 13] Permission denied: 'C:\\Users\\SAHAN\\.keras\\datasets\\cifar-10-batches-py.tar.gz'
4import ssl
5ssl._create_default_https_context = ssl._create_unverified_context
6

Source https://stackoverflow.com/questions/69687794

QUESTION

AssertionError: Tried to export a function which references untracked resource

Asked 2021-Sep-07 at 11:23

I wrote a unit-test in order to safe a model after noticing that I am not able to do so (anymore) during training.

1@pytest.mark.usefixtures("maybe_run_functions_eagerly")
2def test_save_model(speech_model: Tuple[TransducerBase, SpeechFeaturesConfig]):
3    model, speech_features_config = speech_model
4    speech_features_config: SpeechFeaturesConfig
5    channels = 3 if speech_features_config.add_delta_deltas else 1
6    num_mel_bins = speech_features_config.num_mel_bins
7    enc_inputs = np.random.rand(1, 50, num_mel_bins, channels)
8    dec_inputs = np.expand_dims(np.random.randint(0, 25, size=10), axis=1)
9    inputs = enc_inputs, dec_inputs
10    model(inputs)
11
12    # Throws KeyError:
13    # graph = tf.compat.v1.get_default_graph()
14    # tensor = graph.get_tensor_by_name("77040:0")
15
16    directory = tempfile.mkdtemp(prefix=f"{model.__class__.__name__}_")
17    try:
18        model.save(directory)
19    finally:
20        shutil.rmtree(directory)
21

Trying to save the model will always throw the following error:

1@pytest.mark.usefixtures("maybe_run_functions_eagerly")
2def test_save_model(speech_model: Tuple[TransducerBase, SpeechFeaturesConfig]):
3    model, speech_features_config = speech_model
4    speech_features_config: SpeechFeaturesConfig
5    channels = 3 if speech_features_config.add_delta_deltas else 1
6    num_mel_bins = speech_features_config.num_mel_bins
7    enc_inputs = np.random.rand(1, 50, num_mel_bins, channels)
8    dec_inputs = np.expand_dims(np.random.randint(0, 25, size=10), axis=1)
9    inputs = enc_inputs, dec_inputs
10    model(inputs)
11
12    # Throws KeyError:
13    # graph = tf.compat.v1.get_default_graph()
14    # tensor = graph.get_tensor_by_name("77040:0")
15
16    directory = tempfile.mkdtemp(prefix=f"{model.__class__.__name__}_")
17    try:
18        model.save(directory)
19    finally:
20        shutil.rmtree(directory)
21E         AssertionError: Tried to export a function which references untracked resource Tensor("77040:0", shape=(), dtype=resource). TensorFlow objects (e.g. tf.Variable) captured by functions must be tracked by assigning them to an attribute of a tracked object or assigned to an attribute of the main object directly.
22E         
23E         Trackable Python objects referring to this tensor (from gc.get_referrers, limited to two hops):
24E         <tf.Variable 'transformer_transducer/transducer_encoder/inputs_embedding/convolution_stack/conv2d/kernel:0' shape=(3, 3, 3, 32) dtype=float32>
25

Note: As you can see in the code above, but I am not able to retrieve this tensor with tf.compat.v1.get_default_graph().get_tensor_by_name("77040:0").

I tried the following too, but the result is always empty:

1@pytest.mark.usefixtures("maybe_run_functions_eagerly")
2def test_save_model(speech_model: Tuple[TransducerBase, SpeechFeaturesConfig]):
3    model, speech_features_config = speech_model
4    speech_features_config: SpeechFeaturesConfig
5    channels = 3 if speech_features_config.add_delta_deltas else 1
6    num_mel_bins = speech_features_config.num_mel_bins
7    enc_inputs = np.random.rand(1, 50, num_mel_bins, channels)
8    dec_inputs = np.expand_dims(np.random.randint(0, 25, size=10), axis=1)
9    inputs = enc_inputs, dec_inputs
10    model(inputs)
11
12    # Throws KeyError:
13    # graph = tf.compat.v1.get_default_graph()
14    # tensor = graph.get_tensor_by_name("77040:0")
15
16    directory = tempfile.mkdtemp(prefix=f"{model.__class__.__name__}_")
17    try:
18        model.save(directory)
19    finally:
20        shutil.rmtree(directory)
21E         AssertionError: Tried to export a function which references untracked resource Tensor("77040:0", shape=(), dtype=resource). TensorFlow objects (e.g. tf.Variable) captured by functions must be tracked by assigning them to an attribute of a tracked object or assigned to an attribute of the main object directly.
22E         
23E         Trackable Python objects referring to this tensor (from gc.get_referrers, limited to two hops):
24E         <tf.Variable 'transformer_transducer/transducer_encoder/inputs_embedding/convolution_stack/conv2d/kernel:0' shape=(3, 3, 3, 32) dtype=float32>
25model(batch)  # Build the model
26
27tensor_name = "77040"
28
29var_names = [var.name for var in model.trainable_weights]
30weights = list(filter(lambda var: tensor_name in var, var_names))
31
32var_names = [var.name for var in model.trainable_variables]
33variables = list(filter(lambda var: tensor_name in var, var_names))
34
35print(weights)
36print(variables)
37

The problem is that I do not understand why I am getting this because the affected layer is tracked by Keras as you can see in the screenshot below. I took it during a debug-session in the call() function.

enter image description here

I have no explanation for this and I am running out of ideas what the issue might be here.

The transformations list in the screenshot is a property of and getting constructed by a layer InputsEmbedding like so:

1@pytest.mark.usefixtures("maybe_run_functions_eagerly")
2def test_save_model(speech_model: Tuple[TransducerBase, SpeechFeaturesConfig]):
3    model, speech_features_config = speech_model
4    speech_features_config: SpeechFeaturesConfig
5    channels = 3 if speech_features_config.add_delta_deltas else 1
6    num_mel_bins = speech_features_config.num_mel_bins
7    enc_inputs = np.random.rand(1, 50, num_mel_bins, channels)
8    dec_inputs = np.expand_dims(np.random.randint(0, 25, size=10), axis=1)
9    inputs = enc_inputs, dec_inputs
10    model(inputs)
11
12    # Throws KeyError:
13    # graph = tf.compat.v1.get_default_graph()
14    # tensor = graph.get_tensor_by_name("77040:0")
15
16    directory = tempfile.mkdtemp(prefix=f"{model.__class__.__name__}_")
17    try:
18        model.save(directory)
19    finally:
20        shutil.rmtree(directory)
21E         AssertionError: Tried to export a function which references untracked resource Tensor("77040:0", shape=(), dtype=resource). TensorFlow objects (e.g. tf.Variable) captured by functions must be tracked by assigning them to an attribute of a tracked object or assigned to an attribute of the main object directly.
22E         
23E         Trackable Python objects referring to this tensor (from gc.get_referrers, limited to two hops):
24E         <tf.Variable 'transformer_transducer/transducer_encoder/inputs_embedding/convolution_stack/conv2d/kernel:0' shape=(3, 3, 3, 32) dtype=float32>
25model(batch)  # Build the model
26
27tensor_name = "77040"
28
29var_names = [var.name for var in model.trainable_weights]
30weights = list(filter(lambda var: tensor_name in var, var_names))
31
32var_names = [var.name for var in model.trainable_variables]
33variables = list(filter(lambda var: tensor_name in var, var_names))
34
35print(weights)
36print(variables)
37class InputsEmbedding(layers.Layer, TimeReduction):
38    def __init__(self, config: InputsEmbeddingConfig, **kwargs):
39        super().__init__(**kwargs)
40
41        if config.transformations is None or not len(config.transformations):
42            raise RuntimeError("No transformations provided.")
43
44        self.config = config
45
46        self.transformations = list()
47        for transformation in self.config.transformations:
48            layer_name, layer_params = list(transformation.items())[0]
49            layer = _get_layer(layer_name, layer_params)
50            self.transformations.append(layer)
51
52        self.init_time_reduction_layer()
53
54    def get_config(self):
55        return self.config.dict()
56
57
58def _get_layer(name: str, params: dict) -> layers.Layer:
59    if name == "conv2d_stack":
60        return ConvolutionStack(**params)
61    elif name == "stack_frames":
62        return StackFrames(**params)
63    else:
64        raise RuntimeError(f"Unsupported or unknown time-reduction layer {name}")
65

In order to verify that the problem is not the InputsEmbedding, I created a unit-text for saving a model that is using just this particular layer.

1@pytest.mark.usefixtures("maybe_run_functions_eagerly")
2def test_save_model(speech_model: Tuple[TransducerBase, SpeechFeaturesConfig]):
3    model, speech_features_config = speech_model
4    speech_features_config: SpeechFeaturesConfig
5    channels = 3 if speech_features_config.add_delta_deltas else 1
6    num_mel_bins = speech_features_config.num_mel_bins
7    enc_inputs = np.random.rand(1, 50, num_mel_bins, channels)
8    dec_inputs = np.expand_dims(np.random.randint(0, 25, size=10), axis=1)
9    inputs = enc_inputs, dec_inputs
10    model(inputs)
11
12    # Throws KeyError:
13    # graph = tf.compat.v1.get_default_graph()
14    # tensor = graph.get_tensor_by_name("77040:0")
15
16    directory = tempfile.mkdtemp(prefix=f"{model.__class__.__name__}_")
17    try:
18        model.save(directory)
19    finally:
20        shutil.rmtree(directory)
21E         AssertionError: Tried to export a function which references untracked resource Tensor("77040:0", shape=(), dtype=resource). TensorFlow objects (e.g. tf.Variable) captured by functions must be tracked by assigning them to an attribute of a tracked object or assigned to an attribute of the main object directly.
22E         
23E         Trackable Python objects referring to this tensor (from gc.get_referrers, limited to two hops):
24E         <tf.Variable 'transformer_transducer/transducer_encoder/inputs_embedding/convolution_stack/conv2d/kernel:0' shape=(3, 3, 3, 32) dtype=float32>
25model(batch)  # Build the model
26
27tensor_name = "77040"
28
29var_names = [var.name for var in model.trainable_weights]
30weights = list(filter(lambda var: tensor_name in var, var_names))
31
32var_names = [var.name for var in model.trainable_variables]
33variables = list(filter(lambda var: tensor_name in var, var_names))
34
35print(weights)
36print(variables)
37class InputsEmbedding(layers.Layer, TimeReduction):
38    def __init__(self, config: InputsEmbeddingConfig, **kwargs):
39        super().__init__(**kwargs)
40
41        if config.transformations is None or not len(config.transformations):
42            raise RuntimeError("No transformations provided.")
43
44        self.config = config
45
46        self.transformations = list()
47        for transformation in self.config.transformations:
48            layer_name, layer_params = list(transformation.items())[0]
49            layer = _get_layer(layer_name, layer_params)
50            self.transformations.append(layer)
51
52        self.init_time_reduction_layer()
53
54    def get_config(self):
55        return self.config.dict()
56
57
58def _get_layer(name: str, params: dict) -> layers.Layer:
59    if name == "conv2d_stack":
60        return ConvolutionStack(**params)
61    elif name == "stack_frames":
62        return StackFrames(**params)
63    else:
64        raise RuntimeError(f"Unsupported or unknown time-reduction layer {name}")
65@pytest.mark.usefixtures("maybe_run_functions_eagerly")
66def test_inputs_embedding_save_model():
67    convolutions = [
68        "filters=2, kernel_size=(3, 3), strides=(2, 1)",
69        "filters=4, kernel_size=(3, 3), strides=(2, 1)",
70        "filters=8, kernel_size=(3, 4), strides=(1, 1)",
71    ]
72
73    config = InputsEmbeddingConfig()
74    config.transformations = [dict(conv2d_stack=dict(convolutions=convolutions)), dict(stack_frames=dict(n=2))]
75
76    num_features = 8
77    num_channels = 3
78
79    inputs = layers.Input(shape=(None, num_features, num_channels))
80    x = inputs
81    x, _ = InputsEmbedding(config)(x)
82    model = keras.Model(inputs=inputs, outputs=x)
83    model.build(input_shape=(1, 20, num_features, num_channels))
84
85    directory = tempfile.mkdtemp(prefix=f"{model.__class__.__name__}_")
86    try:
87        model.save(directory)
88    finally:
89        shutil.rmtree(directory)
90

Here I am able to save this layer without any issues:

enter image description here

ConvolutionStack

As it seems to be relevant, here is the (rather ugly) implementation of ConvolutionStack:

1@pytest.mark.usefixtures("maybe_run_functions_eagerly")
2def test_save_model(speech_model: Tuple[TransducerBase, SpeechFeaturesConfig]):
3    model, speech_features_config = speech_model
4    speech_features_config: SpeechFeaturesConfig
5    channels = 3 if speech_features_config.add_delta_deltas else 1
6    num_mel_bins = speech_features_config.num_mel_bins
7    enc_inputs = np.random.rand(1, 50, num_mel_bins, channels)
8    dec_inputs = np.expand_dims(np.random.randint(0, 25, size=10), axis=1)
9    inputs = enc_inputs, dec_inputs
10    model(inputs)
11
12    # Throws KeyError:
13    # graph = tf.compat.v1.get_default_graph()
14    # tensor = graph.get_tensor_by_name("77040:0")
15
16    directory = tempfile.mkdtemp(prefix=f"{model.__class__.__name__}_")
17    try:
18        model.save(directory)
19    finally:
20        shutil.rmtree(directory)
21E         AssertionError: Tried to export a function which references untracked resource Tensor("77040:0", shape=(), dtype=resource). TensorFlow objects (e.g. tf.Variable) captured by functions must be tracked by assigning them to an attribute of a tracked object or assigned to an attribute of the main object directly.
22E         
23E         Trackable Python objects referring to this tensor (from gc.get_referrers, limited to two hops):
24E         <tf.Variable 'transformer_transducer/transducer_encoder/inputs_embedding/convolution_stack/conv2d/kernel:0' shape=(3, 3, 3, 32) dtype=float32>
25model(batch)  # Build the model
26
27tensor_name = "77040"
28
29var_names = [var.name for var in model.trainable_weights]
30weights = list(filter(lambda var: tensor_name in var, var_names))
31
32var_names = [var.name for var in model.trainable_variables]
33variables = list(filter(lambda var: tensor_name in var, var_names))
34
35print(weights)
36print(variables)
37class InputsEmbedding(layers.Layer, TimeReduction):
38    def __init__(self, config: InputsEmbeddingConfig, **kwargs):
39        super().__init__(**kwargs)
40
41        if config.transformations is None or not len(config.transformations):
42            raise RuntimeError("No transformations provided.")
43
44        self.config = config
45
46        self.transformations = list()
47        for transformation in self.config.transformations:
48            layer_name, layer_params = list(transformation.items())[0]
49            layer = _get_layer(layer_name, layer_params)
50            self.transformations.append(layer)
51
52        self.init_time_reduction_layer()
53
54    def get_config(self):
55        return self.config.dict()
56
57
58def _get_layer(name: str, params: dict) -> layers.Layer:
59    if name == "conv2d_stack":
60        return ConvolutionStack(**params)
61    elif name == "stack_frames":
62        return StackFrames(**params)
63    else:
64        raise RuntimeError(f"Unsupported or unknown time-reduction layer {name}")
65@pytest.mark.usefixtures("maybe_run_functions_eagerly")
66def test_inputs_embedding_save_model():
67    convolutions = [
68        "filters=2, kernel_size=(3, 3), strides=(2, 1)",
69        "filters=4, kernel_size=(3, 3), strides=(2, 1)",
70        "filters=8, kernel_size=(3, 4), strides=(1, 1)",
71    ]
72
73    config = InputsEmbeddingConfig()
74    config.transformations = [dict(conv2d_stack=dict(convolutions=convolutions)), dict(stack_frames=dict(n=2))]
75
76    num_features = 8
77    num_channels = 3
78
79    inputs = layers.Input(shape=(None, num_features, num_channels))
80    x = inputs
81    x, _ = InputsEmbedding(config)(x)
82    model = keras.Model(inputs=inputs, outputs=x)
83    model.build(input_shape=(1, 20, num_features, num_channels))
84
85    directory = tempfile.mkdtemp(prefix=f"{model.__class__.__name__}_")
86    try:
87        model.save(directory)
88    finally:
89        shutil.rmtree(directory)
90from typing import List
91
92import tensorflow as tf
93from tensorflow.keras import layers
94from tensorflow.python.keras.layers import convolutional
95
96from speech.lab.layers import InputsRequirements
97from speech.lab.models import conv_util, models_util
98
99
100class ConvolutionStack(layers.Layer):
101    def __init__(
102        self,
103        convolutions: List[str],
104        kernel_regularizer: dict = None,
105        bias_regularizer: dict = None,
106        **kwargs
107    ):
108        super().__init__(**kwargs)
109        self.config = dict(
110            convolutions=convolutions,
111            kernel_regularizer=kernel_regularizer,
112            bias_regularizer=bias_regularizer
113        )
114        self.conv_stack_config = [eval(f"dict({convolution})") for convolution in convolutions]
115        self.conv_blocks = list()
116
117        if kernel_regularizer is not None:
118            kernel_regularizer = models_util.maybe_to_regularizer(kernel_regularizer)
119        if bias_regularizer is not None:
120            bias_regularizer = models_util.maybe_to_regularizer(bias_regularizer)
121
122        for block_config in self.conv_stack_config:
123            block = _new_convolution_block(
124                **block_config,
125                kernel_regularizer=kernel_regularizer,
126                bias_regularizer=bias_regularizer,
127            )
128            self.conv_blocks.append(block)
129
130        self.drop_dim2 = layers.Lambda(tf.squeeze, arguments=dict(axis=-2))
131        self.expand_last = layers.Lambda(tf.expand_dims, arguments=dict(axis=-1))
132
133    @property
134    def inputs_requirements(self) -> InputsRequirements:
135        requirements, frame_look_back = conv_util.get_conv2d_stack_requirements(self.conv_stack_config)
136        first = requirements[0]
137        t_min, f_size = first["min_size"]
138        t_grow, f_grow = first["grow_size"]
139        return InputsRequirements(
140            frame_look_back=frame_look_back,
141            t_min=t_min,
142            t_grow=t_grow,
143            f_min=f_size,
144            f_grow=f_grow,
145        )
146
147    def call(self, inputs, training=None, mask=None, **kwargs):
148        """
149        :param inputs:
150            Tensor taking the form [batch, time, freq, channel]
151        :param training:
152        :param mask:
153        :param kwargs:
154        :return:
155            Tensor taking the form [batch, time, freq, 1]
156        """
157
158        if training:
159            t_min = self.inputs_requirements.t_min
160            t_grow = self.inputs_requirements.t_grow
161            pad = conv_util.get_padding_for_loss(tf.shape(inputs)[1], t_min=t_min, t_grow=t_grow)
162            inputs = tf.pad(inputs, ((0, 0), (0, pad), (0, 0), (0, 0)))
163
164            if mask is not None:
165                mask = tf.pad(mask, ((0, 0), (0, pad)))
166
167        f_min = self.inputs_requirements.f_min
168        f_grow = self.inputs_requirements.f_grow
169        assert (inputs.shape[2] - f_min) % f_grow == 0, (
170            f'Inputs dimension "freq" ' f"expected to be {f_min} + n * {f_grow}  but got {inputs.shape[2]} instead."
171        )
172
173        x = inputs
174        for block in self.conv_blocks:
175
176            for layer in block:
177
178                if mask is not None and isinstance(layer, convolutional.Conv):
179                    st, _ = layer.strides
180                    kt = tf.maximum(layer.kernel_size[0] - 1, 1)
181                    mask = mask[:, :-kt][:, ::st]
182                    mask = tf.pad(mask, ((0, 0), (0, tf.maximum(2 - layer.kernel_size[0], 0))))
183
184                x = layer(x, training=training)
185
186        return self.expand_last(self.drop_dim2(x)), mask
187
188    def get_config(self):
189        return self.config
190
191
192def _new_convolution_block(
193    filters: int,
194    kernel_size: tuple,
195    strides: tuple,
196    use_bias: bool = False,
197    use_norm: bool = True,
198    kernel_regularizer=None,
199    bias_regularizer=None,
200    activation=None,
201):
202    assert strides[0] % 2 == 0 or strides[0] == 1, "Strides on the time axis must be divisible by 2 or be exactly 1."
203
204    if activation is not None:
205        activation_layer = layers.Activation(activation)
206    else:
207        activation_layer = layers.Lambda(lambda x: x)
208
209    if use_norm:
210        norm_layer = layers.LayerNormalization()
211    else:
212        norm_layer = layers.Lambda(lambda x: x)
213
214    return (
215        layers.Conv2D(
216            filters=filters,
217            kernel_size=kernel_size,
218            strides=strides,
219            use_bias=use_bias,
220            kernel_regularizer=kernel_regularizer,
221            bias_regularizer=bias_regularizer,
222        ),
223        norm_layer,
224        activation_layer,
225    )
226

ANSWER

Answered 2021-Sep-06 at 13:25

Your issue is not related to 'transformer_transducer/transducer_encoder/inputs_embedding/ convolution_stack/conv2d/kernel:0'.
The error code tells you this element is referring to a non trackable element. It seems the non-trackable object is not directly assigned to an attribute of this conv2d/kernel:0.

To solve your issue, we need to localize Tensor("77040:0", shape=(), dtype=resource) from this error code:

1@pytest.mark.usefixtures("maybe_run_functions_eagerly")
2def test_save_model(speech_model: Tuple[TransducerBase, SpeechFeaturesConfig]):
3    model, speech_features_config = speech_model
4    speech_features_config: SpeechFeaturesConfig
5    channels = 3 if speech_features_config.add_delta_deltas else 1
6    num_mel_bins = speech_features_config.num_mel_bins
7    enc_inputs = np.random.rand(1, 50, num_mel_bins, channels)
8    dec_inputs = np.expand_dims(np.random.randint(0, 25, size=10), axis=1)
9    inputs = enc_inputs, dec_inputs
10    model(inputs)
11
12    # Throws KeyError:
13    # graph = tf.compat.v1.get_default_graph()
14    # tensor = graph.get_tensor_by_name("77040:0")
15
16    directory = tempfile.mkdtemp(prefix=f"{model.__class__.__name__}_")
17    try:
18        model.save(directory)
19    finally:
20        shutil.rmtree(directory)
21E         AssertionError: Tried to export a function which references untracked resource Tensor("77040:0", shape=(), dtype=resource). TensorFlow objects (e.g. tf.Variable) captured by functions must be tracked by assigning them to an attribute of a tracked object or assigned to an attribute of the main object directly.
22E         
23E         Trackable Python objects referring to this tensor (from gc.get_referrers, limited to two hops):
24E         <tf.Variable 'transformer_transducer/transducer_encoder/inputs_embedding/convolution_stack/conv2d/kernel:0' shape=(3, 3, 3, 32) dtype=float32>
25model(batch)  # Build the model
26
27tensor_name = "77040"
28
29var_names = [var.name for var in model.trainable_weights]
30weights = list(filter(lambda var: tensor_name in var, var_names))
31
32var_names = [var.name for var in model.trainable_variables]
33variables = list(filter(lambda var: tensor_name in var, var_names))
34
35print(weights)
36print(variables)
37class InputsEmbedding(layers.Layer, TimeReduction):
38    def __init__(self, config: InputsEmbeddingConfig, **kwargs):
39        super().__init__(**kwargs)
40
41        if config.transformations is None or not len(config.transformations):
42            raise RuntimeError("No transformations provided.")
43
44        self.config = config
45
46        self.transformations = list()
47        for transformation in self.config.transformations:
48            layer_name, layer_params = list(transformation.items())[0]
49            layer = _get_layer(layer_name, layer_params)
50            self.transformations.append(layer)
51
52        self.init_time_reduction_layer()
53
54    def get_config(self):
55        return self.config.dict()
56
57
58def _get_layer(name: str, params: dict) -> layers.Layer:
59    if name == "conv2d_stack":
60        return ConvolutionStack(**params)
61    elif name == "stack_frames":
62        return StackFrames(**params)
63    else:
64        raise RuntimeError(f"Unsupported or unknown time-reduction layer {name}")
65@pytest.mark.usefixtures("maybe_run_functions_eagerly")
66def test_inputs_embedding_save_model():
67    convolutions = [
68        "filters=2, kernel_size=(3, 3), strides=(2, 1)",
69        "filters=4, kernel_size=(3, 3), strides=(2, 1)",
70        "filters=8, kernel_size=(3, 4), strides=(1, 1)",
71    ]
72
73    config = InputsEmbeddingConfig()
74    config.transformations = [dict(conv2d_stack=dict(convolutions=convolutions)), dict(stack_frames=dict(n=2))]
75
76    num_features = 8
77    num_channels = 3
78
79    inputs = layers.Input(shape=(None, num_features, num_channels))
80    x = inputs
81    x, _ = InputsEmbedding(config)(x)
82    model = keras.Model(inputs=inputs, outputs=x)
83    model.build(input_shape=(1, 20, num_features, num_channels))
84
85    directory = tempfile.mkdtemp(prefix=f"{model.__class__.__name__}_")
86    try:
87        model.save(directory)
88    finally:
89        shutil.rmtree(directory)
90from typing import List
91
92import tensorflow as tf
93from tensorflow.keras import layers
94from tensorflow.python.keras.layers import convolutional
95
96from speech.lab.layers import InputsRequirements
97from speech.lab.models import conv_util, models_util
98
99
100class ConvolutionStack(layers.Layer):
101    def __init__(
102        self,
103        convolutions: List[str],
104        kernel_regularizer: dict = None,
105        bias_regularizer: dict = None,
106        **kwargs
107    ):
108        super().__init__(**kwargs)
109        self.config = dict(
110            convolutions=convolutions,
111            kernel_regularizer=kernel_regularizer,
112            bias_regularizer=bias_regularizer
113        )
114        self.conv_stack_config = [eval(f"dict({convolution})") for convolution in convolutions]
115        self.conv_blocks = list()
116
117        if kernel_regularizer is not None:
118            kernel_regularizer = models_util.maybe_to_regularizer(kernel_regularizer)
119        if bias_regularizer is not None:
120            bias_regularizer = models_util.maybe_to_regularizer(bias_regularizer)
121
122        for block_config in self.conv_stack_config:
123            block = _new_convolution_block(
124                **block_config,
125                kernel_regularizer=kernel_regularizer,
126                bias_regularizer=bias_regularizer,
127            )
128            self.conv_blocks.append(block)
129
130        self.drop_dim2 = layers.Lambda(tf.squeeze, arguments=dict(axis=-2))
131        self.expand_last = layers.Lambda(tf.expand_dims, arguments=dict(axis=-1))
132
133    @property
134    def inputs_requirements(self) -> InputsRequirements:
135        requirements, frame_look_back = conv_util.get_conv2d_stack_requirements(self.conv_stack_config)
136        first = requirements[0]
137        t_min, f_size = first["min_size"]
138        t_grow, f_grow = first["grow_size"]
139        return InputsRequirements(
140            frame_look_back=frame_look_back,
141            t_min=t_min,
142            t_grow=t_grow,
143            f_min=f_size,
144            f_grow=f_grow,
145        )
146
147    def call(self, inputs, training=None, mask=None, **kwargs):
148        """
149        :param inputs:
150            Tensor taking the form [batch, time, freq, channel]
151        :param training:
152        :param mask:
153        :param kwargs:
154        :return:
155            Tensor taking the form [batch, time, freq, 1]
156        """
157
158        if training:
159            t_min = self.inputs_requirements.t_min
160            t_grow = self.inputs_requirements.t_grow
161            pad = conv_util.get_padding_for_loss(tf.shape(inputs)[1], t_min=t_min, t_grow=t_grow)
162            inputs = tf.pad(inputs, ((0, 0), (0, pad), (0, 0), (0, 0)))
163
164            if mask is not None:
165                mask = tf.pad(mask, ((0, 0), (0, pad)))
166
167        f_min = self.inputs_requirements.f_min
168        f_grow = self.inputs_requirements.f_grow
169        assert (inputs.shape[2] - f_min) % f_grow == 0, (
170            f'Inputs dimension "freq" ' f"expected to be {f_min} + n * {f_grow}  but got {inputs.shape[2]} instead."
171        )
172
173        x = inputs
174        for block in self.conv_blocks:
175
176            for layer in block:
177
178                if mask is not None and isinstance(layer, convolutional.Conv):
179                    st, _ = layer.strides
180                    kt = tf.maximum(layer.kernel_size[0] - 1, 1)
181                    mask = mask[:, :-kt][:, ::st]
182                    mask = tf.pad(mask, ((0, 0), (0, tf.maximum(2 - layer.kernel_size[0], 0))))
183
184                x = layer(x, training=training)
185
186        return self.expand_last(self.drop_dim2(x)), mask
187
188    def get_config(self):
189        return self.config
190
191
192def _new_convolution_block(
193    filters: int,
194    kernel_size: tuple,
195    strides: tuple,
196    use_bias: bool = False,
197    use_norm: bool = True,
198    kernel_regularizer=None,
199    bias_regularizer=None,
200    activation=None,
201):
202    assert strides[0] % 2 == 0 or strides[0] == 1, "Strides on the time axis must be divisible by 2 or be exactly 1."
203
204    if activation is not None:
205        activation_layer = layers.Activation(activation)
206    else:
207        activation_layer = layers.Lambda(lambda x: x)
208
209    if use_norm:
210        norm_layer = layers.LayerNormalization()
211    else:
212        norm_layer = layers.Lambda(lambda x: x)
213
214    return (
215        layers.Conv2D(
216            filters=filters,
217            kernel_size=kernel_size,
218            strides=strides,
219            use_bias=use_bias,
220            kernel_regularizer=kernel_regularizer,
221            bias_regularizer=bias_regularizer,
222        ),
223        norm_layer,
224        activation_layer,
225    )
226AssertionError: Tried to export a function which references untracked resource\
227Tensor("77040:0", shape=(), dtype=resource). 
228TensorFlow objects (e.g. tf.Variable) captured by functions must be tracked by assigning them to an attribute of a tracked object or assigned to an attribute of the main object directly.
229
Edit:

Thanks to your comments, we found that "ConvolutionStack" seems to reproduce the error.

The problem only occurs if I use the ConvolutionStack layer in InputsEmbedding but I can save both of them successfully in a standalone model.

I understand you cannot share the config of this layer and that's why I suggest you try and localize this Tensor(77040:0 from the ConvolutionStack.

This untrackable tensor must be an artifcat or a temporary tensor created by a process of a function of ConvolutionStack.

Try to find a tensor that could be passed from a function to another instead of being assigned to an attribute of a layer's class

Source https://stackoverflow.com/questions/69040420

QUESTION

Tensorflow - Multi-GPU doesn’t work for model(inputs) nor when computing the gradients

Asked 2021-Jul-16 at 07:14

When using multiple GPUs to perform inference on a model (e.g. the call method: model(inputs)) and calculate its gradients, the machine only uses one GPU, leaving the rest idle.

For example in this code snippet below:

1import tensorflow as tf
2import numpy as np
3import os
4
5os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
6
7# Make the tf-data
8path_filename_records = 'your_path_to_records'
9bs = 128
10
11dataset = tf.data.TFRecordDataset(path_filename_records)
12dataset = (dataset
13           .map(parse_record, num_parallel_calls=tf.data.experimental.AUTOTUNE)
14           .batch(bs)
15           .prefetch(tf.data.experimental.AUTOTUNE)
16          )
17
18# Load model trained using MirroredStrategy
19path_to_resnet = 'your_path_to_resnet'
20mirrored_strategy = tf.distribute.MirroredStrategy()
21with mirrored_strategy.scope():
22    resnet50 = tf.keras.models.load_model(path_to_resnet)
23
24for pre_images, true_label in dataset:
25    with tf.GradientTape() as tape:
26       tape.watch(pre_images)
27       outputs = resnet50(pre_images)
28       grads = tape.gradient(outputs, pre_images)
29

Only one GPU is used. You can profile the behavior of the GPUs with nvidia-smi. I don't know if it is supposed to be like this, both the model(inputs) and tape.gradient to not have multi-GPU support. But if it is, then it's a big problem because if you have a large dataset and need to calculate the gradients with respect to the inputs (e.g. interpretability porpuses) it might take days with one GPU. Another thing I tried was using model.predict() but this isn't possible with tf.GradientTape.

What I've tried so far and didn't work

  1. Put all the code inside mirrored strategy scope.
  2. Used different GPUs: I've tried A100, A6000 and RTX5000. Also changed the number of graphic cards and varied the batch size.
  3. Specified a list of GPUs, for instance, strategy = tf.distribute.MirroredStrategy(['/gpu:0', '/gpu:1']).
  4. Added this strategy = tf.distribute.MirroredStrategy(cross_device_ops=tf.distribute.HierarchicalCopyAllReduce()) as @Kaveh suggested.

How do I know that only one GPU is working?

I used the command watch -n 1 nvidia-smi in the terminal and observed that only one GPU is at 100%, the rest are at 0%.

Working Example

You can find a working example with a CNN trained on the dogs_vs_cats datasets below. You won't need to manually download the dataset as I used the tfds version, nor train a model.

Notebook: Working Example.ipynb

Saved Model:

ANSWER

Answered 2021-Jul-16 at 07:14

It is supposed to run in single gpu (probably the first gpu, GPU:0) for any codes that are outside of mirrored_strategy.run(). Also, as you want to have the gradients returned from replicas, mirrored_strategy.gather() is needed as well.

Besides these, a distributed dataset must be created by using mirrored_strategy.experimental_distribute_dataset. Distributed dataset tries to distribute single batch of data across replicas evenly. An example about these points is included below.

model.fit(), model.predict(),and etc... run in distributed manner automatically just because they've already handled everything mentioned above for you.

Example codes:

1import tensorflow as tf
2import numpy as np
3import os
4
5os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
6
7# Make the tf-data
8path_filename_records = 'your_path_to_records'
9bs = 128
10
11dataset = tf.data.TFRecordDataset(path_filename_records)
12dataset = (dataset
13           .map(parse_record, num_parallel_calls=tf.data.experimental.AUTOTUNE)
14           .batch(bs)
15           .prefetch(tf.data.experimental.AUTOTUNE)
16          )
17
18# Load model trained using MirroredStrategy
19path_to_resnet = 'your_path_to_resnet'
20mirrored_strategy = tf.distribute.MirroredStrategy()
21with mirrored_strategy.scope():
22    resnet50 = tf.keras.models.load_model(path_to_resnet)
23
24for pre_images, true_label in dataset:
25    with tf.GradientTape() as tape:
26       tape.watch(pre_images)
27       outputs = resnet50(pre_images)
28       grads = tape.gradient(outputs, pre_images)
29mirrored_strategy = tf.distribute.MirroredStrategy()
30print(f'using distribution strategy\nnumber of gpus:{mirrored_strategy.num_replicas_in_sync}')
31
32dataset=tf.data.Dataset.from_tensor_slices(np.random.rand(64,224,224,3)).batch(8)
33
34#create distributed dataset
35ds = mirrored_strategy.experimental_distribute_dataset(dataset)
36
37#make variables mirrored
38with mirrored_strategy.scope():
39  resnet50=tf.keras.applications.resnet50.ResNet50()
40
41def step_fn(pre_images):
42  with tf.GradientTape(watch_accessed_variables=False) as tape:
43       tape.watch(pre_images)
44       outputs = resnet50(pre_images)[:,0:1]
45  return tf.squeeze(tape.batch_jacobian(outputs, pre_images))
46
47#define distributed step function using strategy.run and strategy.gather
48@tf.function
49def distributed_step_fn(pre_images):
50  per_replica_grads = mirrored_strategy.run(step_fn, args=(pre_images,))
51  return mirrored_strategy.gather(per_replica_grads,0)
52
53#loop over distributed dataset with distributed_step_fn
54for result in map(distributed_step_fn,ds):
55  print(result.numpy().shape)
56

Source https://stackoverflow.com/questions/68283519

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Keras

Tutorials and Learning Resources are not available at this moment for Keras

Share this Page

share link

Get latest updates on Keras