Zengi
Zengi

Reputation: 121

Error when trying to send neural net generated image in flask

I'm making a basic api to request for generated images from a generator model from pytorch. I've done this using flask and I'm running it locally on MacOS. Everything works and the image returns but then python quits unexpectedly. Here is the code and the error:

Error:

   2019-03-25 16:21:23.514 Python[78776:1407049] WARNING: NSWindow drag regions should only be invalidated on the Main Thread! This will throw an exception in the future. Called from (
    0   AppKit                              0x00007fff4f96fccc -[NSWindow(NSWindow_Theme) _postWindowNeedsToResetDragMarginsUnlessPostingDisabled] + 386
    1   AppKit                              0x00007fff4f96d07c -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1488
    2   AppKit                              0x00007fff4f96caa6 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 45
    3   _macosx.cpython-37m-darwin.so       0x000000010fe634c0 -[Window initWithContentRect:styleMask:backing:defer:withManager:] + 80
    4   _macosx.cpython-37m-darwin.so       0x000000010fe66a17 FigureManager_init + 327
    5   Python                              0x00000001023780bc wrap_init + 12
    6   Python                              0x000000010232fe09 wrapperdescr_call + 121
    7   Python                              0x0000000102328ae1 _PyObject_FastCallKeywords + 433
    8   Python                              0x00000001023e76d4 call_function + 420
    9   Python                              0x00000001023e47d6 _PyEval_EvalFrameDefault + 25190
    10  Python                              0x0000000102329100 function_code_fastcall + 128
    11  Python                              0x00000001023286f4 _PyFunction_FastCallDict + 148
    12  Python                              0x0000000102329b3f _PyObject_Call_Prepend + 143
    13  Python                              0x0000000102378001 slot_tp_init + 145
    14  Python                              0x0000000102373959 type_call + 297
    15  Python                              0x0000000102328ae1 _PyObject_FastCallKeywords + 433
    16  Python                              0x00000001023e76d4 call_function + 420
    17  Python                              0x00000001023e47d6 _PyEval_EvalFrameDefault + 25190
    18  Python                              0x0000000102329100 function_code_fastcall + 128
    19  Python                              0x00000001023e7812 call_function + 738
    20  Python                              0x00000001023e47d6 _PyEval_EvalFrameDefault + 25190
    21  Python                              0x00000001023e8336 _PyEval_EvalCodeWithName + 2422
    22  Python                              0x000000010232886b _PyFunction_FastCallDict + 523
    23  Python                              0x0000000102329b3f _PyObject_Call_Prepend + 143
    24  Python                              0x0000000102328df7 PyObject_Call + 135
    25  Python                              0x00000001023e4ae7 _PyEval_EvalFrameDefault + 25975
    26  Python                              0x00000001023e8336 _PyEval_EvalCodeWithName + 2422
    27  Python                              0x0000000102328c91 _PyFunction_FastCallKeywords + 257
    28  Python                              0x00000001023e7812 call_function + 738
    29  Python                              0x00000001023e4877 _PyEval_EvalFrameDefault + 25351
    30  Python                              0x0000000102329100 function_code_fastcall + 128
    31  Python                              0x00000001023e7812 call_function + 738
    32  Python                              0x00000001023e4877 _PyEval_EvalFrameDefault + 25351
    33  Python                              0x00000001023e8336 _PyEval_EvalCodeWithName + 2422
    34  Python                              0x0000000102328c91 _PyFunction_FastCallKeywords + 257
    35  Python                              0x00000001023e7812 call_function + 738
    36  Python                              0x00000001023e4877 _PyEval_EvalFrameDefault + 25351
    37  Python                              0x00000001023e8336 _PyEval_EvalCodeWithName + 2422
    38  Python                              0x0000000102328c91 _PyFunction_FastCallKeywords + 257
    39  Python                              0x00000001023e7812 call_function + 738
    40  Python                              0x00000001023e47d6 _PyEval_EvalFrameDefault + 25190
    41  Python                              0x0000000102329100 function_code_fastcall + 128
    42  Python                              0x00000001023e7812 call_function + 738
    43  Python                              0x00000001023e47d6 _PyEval_EvalFrameDefault + 25190
    44  Python                              0x00000001023e8336 _PyEval_EvalCodeWithName + 2422
    45  Python                              0x000000010232886b _PyFunction_FastCallDict + 523
    46  Python                              0x00000001023e4ae7 _PyEval_EvalFrameDefault + 25975
    47  Python                              0x0000000102329100 function_code_fastcall + 128
    48  Python                              0x00000001023e7812 call_function + 738
    49  Python                              0x00000001023e47bc _PyEval_EvalFrameDefault + 25164
    50  Python                              0x0000000102329100 function_code_fastcall + 128
    51  Python                              0x00000001023e7812 call_function + 738
    52  Python                              0x00000001023e47bc _PyEval_EvalFrameDefault + 25164
    53  Python                              0x0000000102329100 function_code_fastcall + 128
    54  Python                              0x00000001023e7812 call_function + 738
    55  Python                              0x00000001023e47bc _PyEval_EvalFrameDefault + 25164
    56  Python                              0x0000000102329100 function_code_fastcall + 128
    57  Python                              0x00000001023286f4 _PyFunction_FastCallDict + 148
    58  Python                              0x0000000102329b3f _PyObject_Call_Prepend + 143
    59  Python                              0x0000000102376bc6 slot_tp_call + 150
    60  Python                              0x0000000102328ae1 _PyObject_FastCallKeywords + 433
    61  Python                              0x00000001023e76d4 call_function + 420
    62  Python                              0x00000001023e47d6 _PyEval_EvalFrameDefault + 25190
    63  Python                              0x00000001023370de gen_send_ex + 206
    64  Python                              0x00000001023e3fb8 _PyEval_EvalFrameDefault + 23112
    65  Python                              0x00000001023e8336 _PyEval_EvalCodeWithName + 2422
    66  Python                              0x0000000102328c91 _PyFunction_FastCallKeywords + 257
    67  Python                              0x00000001023e7812 call_function + 738
    68  Python                              0x00000001023e4877 _PyEval_EvalFrameDefault + 25351
    69  Python                              0x00000001023e8336 _PyEval_EvalCodeWithName + 2422
    70  Python                              0x0000000102328c91 _PyFunction_FastCallKeywords + 257
    71  Python                              0x00000001023e7812 call_function + 738
    72  Python                              0x00000001023e47bc _PyEval_EvalFrameDefault + 25164
    73  Python                              0x0000000102329100 function_code_fastcall + 128
    74  Python                              0x00000001023e7812 call_function + 738
    75  Python                              0x00000001023e47bc _PyEval_EvalFrameDefault + 25164
    76  Python                              0x0000000102329100 function_code_fastcall + 128
    77  Python                              0x00000001023e7812 call_function + 738
    78  Python                              0x00000001023e47d6 _PyEval_EvalFrameDefault + 25190
    79  Python                              0x0000000102329100 function_code_fastcall + 128
    80  Python                              0x00000001023e7812 call_function + 738
    81  Python                              0x00000001023e47bc _PyEval_EvalFrameDefault + 25164
    82  Python                              0x0000000102329100 function_code_fastcall + 128
    83  Python                              0x00000001023286f4 _PyFunction_FastCallDict + 148
    84  Python                              0x0000000102329b3f _PyObject_Call_Prepend + 143
    85  Python                              0x0000000102378001 slot_tp_init + 145
    86  Python                              0x0000000102373959 type_call + 297
    87  Python                              0x0000000102328ae1 _PyObject_FastCallKeywords + 433
    88  Python                              0x00000001023e76d4 call_function + 420
    89  Python                              0x00000001023e47d6 _PyEval_EvalFrameDefault + 25190
    90  Python                              0x0000000102329100 function_code_fastcall + 128
    91  Python                              0x00000001023e7812 call_function + 738
    92  Python                              0x00000001023e47bc _PyEval_EvalFrameDefault + 25164
    93  Python                              0x0000000102329100 function_code_fastcall + 128
    94  Python                              0x00000001023286f4 _PyFunction_FastCallDict + 148
    95  Python                              0x0000000102329b3f _PyObject_Call_Prepend + 143
    96  Python                              0x0000000102328df7 PyObject_Call + 135
    97  Python                              0x00000001023e4ae7 _PyEval_EvalFrameDefault + 25975
    98  Python                              0x0000000102329100 function_code_fastcall + 128
    99  Python                              0x00000001023e7812 call_function + 738
    100 Python                              0x00000001023e47bc _PyEval_EvalFrameDefault + 25164
    101 Python                              0x0000000102329100 function_code_fastcall + 128
    102 Python                              0x00000001023e7812 call_function + 738
    103 Python                              0x00000001023e47bc _PyEval_EvalFrameDefault + 25164
    104 Python                              0x0000000102329100 function_code_fastcall + 128
    105 Python                              0x00000001023286f4 _PyFunction_FastCallDict + 148
    106 Python                              0x0000000102329b3f _PyObject_Call_Prepend + 143
    107 Python                              0x0000000102328df7 PyObject_Call + 135
    108 Python                              0x000000010246fbd7 t_bootstrap + 71
    109 Python                              0x0000000102426819 pythread_wrapper + 25
    110 libsystem_pthread.dylib             0x00007fff7f7ce305 _pthread_body + 126
    111 libsystem_pthread.dylib             0x00007fff7f7d126f _pthread_start + 70
    112 libsystem_pthread.dylib             0x00007fff7f7cd415 thread_start + 13
)
127.0.0.1 - - [25/Mar/2019 16:21:23] "GET /sdfa HTTP/1.1" 200 -
Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentlyBuildingLayerTree), function NSViewSetCurrentlyBuildingLayerTreeForDisplay, file /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1671.20.108/AppKit.subproj/NSView.m, line 14143

Main:

from flask import Flask, request, send_file
import loadModel

app = Flask(__name__)


@app.route('/')
def index():
    return 'this is the homepage'

@app.route('/<ganType>')
def generate(ganType):
    loadModel.loadModel()
    return send_file('fakes.jpg')


if __name__ == "__main__":
    app.run(debug=True)

Load Model:

import torch
import torch.nn as nn
import torchvision.utils as vutils
import matplotlib.pyplot as plt
import numpy as np

batch_size = 100
image_size = 64
nc = 3
nz = 100
ngf = 64
ndf = 64
num_epochs = 1
lr = 0.0002
beta1 = 0.5


class Generator(nn.Module): # nn.Module is the base class for all neural net modules.
    def __init__(self):
        super(Generator, self).__init__() # Calls the parent's initialization method

        self.main = nn.Sequential(
             # input is Z, going into a convolution
            nn.ConvTranspose2d( nz, ngf * 8, 4, 1, 0, bias=False),
            nn.BatchNorm2d(ngf * 8),
            nn.ReLU(True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf * 4),
            nn.ReLU(True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d( ngf * 4, ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf * 2),
            nn.ReLU(True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d( ngf * 2, ngf, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf),
            nn.ReLU(True),
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d( ngf, nc, 4, 2, 1, bias=False),
            nn.Tanh()
            # state size. (nc) x 64 x 64
        )

    def forward(self, input):
        return self.main(input)


def loadModel():


    # device = torch.device("cpu")
    model = Generator()
    model.load_state_dict(torch.load('generator.pt', map_location='cpu'))

    model.eval()
    first_list = []
    img_list = []
    # Generate some images to test the model.

    noise = torch.randn(64, nz, 1, 1)
    fake = model(noise).detach().cpu()
    # inception_img_list.append(np.transpose(fake[0],(1,2,0)))
    img_list.append(vutils.make_grid(fake, padding=2, normalize=True))

    # img_list.append(first_list)
    # plt.subplot(1,2,2)
    plt.axis("off")
    plt.title("Fake Images")
    plt.imsave('fakes.jpg', np.transpose(img_list[-1],(1,2,0)))

    return 'hello'


if __name__ == "__main__":

    loadModel()

As I said, it actually works and returns the generated image. It just also crashes python. I'm not sure if I am doing something incorrectly or if its a MacOs problem. Any help would be appreciated.

Upvotes: 0

Views: 551

Answers (1)

Zengi
Zengi

Reputation: 121

I worked out from people having the same problem: https://github.com/matplotlib/matplotlib/issues/11094

Closing plt before returning fixes the error.

Upvotes: 1

Related Questions